470,636 Members | 1,610 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Unix fold command in python

Hello Group:

Hopefully someone can answer my question.

I have a unix shell command that I would like to emulate in python. I
am sanning a file that contains a stream of data with a record size of
242 bytes but no record delimiters. There are multiple fields in each
record that can be mapped according to their position:

example

field1 byte 0-4
field2 byte 5-8

How do I make python read a record in and report the contents of a
particular field (and may be carry out an operations with that field).

Much appreciated

regards

dean
Jul 18 '05 #1
3 2121
Something like this works for me (not tested).

I'll of course this will just return the character
values of the fields. I'll leave the conversion
to int's, float's or any other value types you
many need to you. If any of the bytes contain
binary data, you will need the struct module to
decode them.

Larry Bates
Syscon, Inc.
class record:
def __init__(self, data=None):
#
# User can either pass the data at class
# creation or later by calling parse.
#
self._fields=(('field1', 0, 4),
('field2', 5, 8),
...
('fieldn',238,242))

if data is not None: self._parse(data)
return

def parse(self, data):
D=self.__dict__
for fieldname, begin, end in self._fields:
D[fieldname]=data[begin, end+1]

return

if __name__=="__main__":

#
# Open file and read all lines, if this is a huge
# file you will need to do this differently by using
# xreadlines inside the loop below.
#
fp=open(inputfile, 'r')
lines=fp.readlines()
fp.close()
#
# Create an instance of your record class
#
RECORD=record()

for line in lines:
RECORD.parse(line)
print RECORD.field1
print RECORD.field2
...
print RECORD.fieldn
"dean" <di******@uku.co.uk> wrote in message
news:70**************************@posting.google.c om...
Hello Group:

Hopefully someone can answer my question.

I have a unix shell command that I would like to emulate in python. I
am sanning a file that contains a stream of data with a record size of
242 bytes but no record delimiters. There are multiple fields in each
record that can be mapped according to their position:

example

field1 byte 0-4
field2 byte 5-8
...
...
... fieldn byte 238-242
How do I make python read a record in and report the contents of a
particular field (and may be carry out an operations with that field).

Much appreciated

regards

dean

Jul 18 '05 #2
On Mon, 14 Jun 2004 03:50:06 -0700, dean wrote:
Hello Group:

Hopefully someone can answer my question.

I have a unix shell command that I would like to emulate in python. I
am sanning a file that contains a stream of data with a record size of
242 bytes but no record delimiters. There are multiple fields in each
record that can be mapped according to their position:

example

field1 byte 0-4
field2 byte 5-8

How do I make python read a record in and report the contents of a
particular field (and may be carry out an operations with that field).

Much appreciated

regards

dean


isn't this just a slice?

like so?

alllines = file.readlines()
for each in alllines:
field1 = each[:4]
field2 = each[4:8]
Jul 18 '05 #3
On Tue, 22 Jun 2004 22:31:34 -0500, David Duncan
<dd*****@rubackedup.com> declaimed the following in comp.lang.python:
isn't this just a slice?

like so?

alllines = file.readlines()
for each in alllines:
field1 = each[:4]
field2 = each[4:8]
If the data is character strings, probably... Otherwise, I'd
suggest the struct module...

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Paddy McCarthy | last post: by
3 posts views Thread by Jay O'Connor | last post: by
4 posts views Thread by RosalieM | last post: by
3 posts views Thread by Kory Wheatley | last post: by
10 posts views Thread by Kotlin Sam | last post: by
23 posts views Thread by Mark Dickinson | last post: by
2 posts views Thread by timdoyle05 | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.