[Tutor] re.format a file

A.T.Hofkamp a.t.hofkamp at tue.nl
Thu Feb 26 15:16:35 CET 2009


prasad rao wrote:
> helloThank you Lie and Kent.
> I forgot  about newline character and the fact that string can be sliced.
> Thanks for your timely help
> BTW I have gone through  the Python library reference and find no examples
> in fileinput module.

The fileinput module only deals with reading and writing data from/to files, 
it does not deal with manipulating that data.

How to manipulate strings is in the 'strings' or 'text' section of a tutorial.

> z=fileinput.input(file,inplace=1)
> for line in  z:
> ???if len(line)<60:pass
> ???if len(line)>60:
> ??????line=line[:60]+'\n'+line[60:]
> Is it the right way to do?

A nice step forward, I'd say.

Did you consider what to do with ridiculous long lines, eg 200, 500, 1000 or 
10000 characters long?
If you want to deal with them, you'd need to repeatedly split the line. You 
could use a while loop for it.

Sincerely,
Albert



More information about the Tutor mailing list