[Tutor] Re: striping whitespace from files
Lee Harr
missive at hotmail.com
Sat Jul 10 17:00:39 CEST 2004
>question about striping white spaces from a file. my
>logic is that each element of the list [i] iterates
>and the whitespace it encounters is stripped by strip
>function.
>I do not know where I am going wrong. Is my logic
>incorrect or it is the way that my syntax is wrong.
>Can any one please help.
>>>f1 = open("citrate.txt",'r')
>>>f2 = f1.read()
>>>from string import strip, split
>>>f2 = f1.readlines()
>>>list = split(f2,'\n')
>>>for i in fstrings:
f1strings = string.split([i])
print f1strings
Use the interactivity of the interpreter to your advantage:
>>>f1 = open('afile.txt')
>>>f2 = f1.read()
>>>f2
'12345\n22345\n32345\n'
>>>lines = f2.split('\n')
>>>lines
['12345', '22345', '32345', '']
>>>f2 = f1.readlines()
>>>f2
[]
>>>f1.close()
>>>f1 = open('afile.txt')
>>>f2 = f1.readlines()
>>>f2
['12345\n', '22345\n', '32345\n']
>>>f1.close()
>>>for line in open('afile.txt'):
... line
...
'12345\n'
'22345\n'
'32345\n'
>>>f1.close()
>>>for line in open('afile.txt'):
... stripped = line.strip()
... stripped
...
'12345'
'22345'
'32345'
_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail
More information about the Tutor
mailing list