[Tutor] Reading from a file
Lloyd Hugh Allen
Sat, 18 Aug 2001 09:54:55 -0400
On Fri, 17 Aug 2001 13:10:24 -0500 (CDT), robert frank brenart wrote:
I'm reading in from a file line by line and splitting up the information
based on commas... however, the last item in every line has an /012
attached to it... i.e.
comes out as
['"CVR"', '7', '3\012']
Just wondering how I can get rid of that \012.
There is a string method "replace" which will do the trick nicely (make
sure you run it before you split the string, as it's a string method,
not a list method):
>>> mystring = "Hi there I'm a string"
>>> mystring.replace('e', '')
"Hi thr I'm a string"
only instead of using the arguments ('e', ''), you'll use ("\012",'').
Windows translates \012 to \n, suggesting that the .strip method may
also be of service (and may be more generalizable if you tend get other
weird "junk" characters). I take it that you do not want to retain the
original line splits...if you do, you may want to split using the
separator "\012" initially.