[Tutor] Reading from a file

Israel Evans israel@lith.com
Fri, 17 Aug 2001 11:47:41 -0700


That's what I was thinking..

How about:

>>> import re
>>> pat = re.compile('\012')
>>> line = """"CVR",1,2\012"""
>>> info = re.sub(pat, '',line)
>>> finalanswer = info.split(',')
>>> finalanswer
['"CVR"', '1', '2']

-----Original Message-----
From: dman [mailto:dsh8290@rit.edu]
Sent: Friday, August 17, 2001 11:43 AM
To: tutor@python.org
Subject: Re: [Tutor] Reading from a file


On Fri, Aug 17, 2001 at 01:10:24PM -0500, 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.
| 
| "CVR",1,2
| 
| comes out as
| 
| ['"CVR"', '7', '3\012']
| 
| Just wondering how I can get rid of that \012.

The \0 part means that that character is being displayed in its octal
format.  012 is '\n' or NL.  What OS created the files and what OS
are you reading them on?  You could strip that character off the
string before you split it.


for line in file.xreadlines() :
    line = line.strip()
    print line.split( "," )

HTH,
-D


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor