Unix fold command in python

David Duncan dduncan at rubackedup.com
Wed Jun 23 05:31:34 CEST 2004


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]



More information about the Python-list mailing list