[Python-de] csv mit <type 'str'>
Falk Friedrich
frodo at prima.de
Die Feb 24 20:09:44 CET 2004
On Di Februar 24 2004 18:24, Falk Friedrich wrote:
>
>Python 2.3: alles prima
>Python 2.2: Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AttributeError: 'module' object has no attribute 'reader'
Ok. Habe inzwischen rausgefunden, daß python 2.2 ein ganz anderes
csv-Modul hat und es dort parse heißt.
Funktioniert (s.u.) auch.
Das einzige, was ich bisher nicht rausgefunden habe ist, wie man dem
parser sagt, daß er ";" statt "," nehmen soll.
TIA, Falk
#!/usr/bin/python2.2
import csv
import StringIO
#text = "aaaa;\"bb\nbb\";cccc\ndddd;eeeee;fffff\n"
text = "aaaa,\"bb\nbb\",cccc\ndddd,eeeee,fffff\n"
file = StringIO.StringIO(text)
data = []
p = csv.parser()
while 1:
line = file.readline()
if not line:
break
fields = p.parse(line)
data.append(fields)
if not fields:
# multi-line record
continue
# process the fields
print data