CSV Module
Moshe Zadka
moshez at math.huji.ac.il
Sat Apr 24 13:41:16 EDT 1999
I wonder if there is some standard module out there that can parse
``CSV'' files. Or, if anyone knows where the documentation of that format
can be had.
Here is my meek attempt at an answer. It is not certain to work, as I am
not sure about the format -- I was guessing from looking at the file.
-------------- begin python code ------------------
import re
csv_entry=re.compile('(?:"[^"]*",)|(?:[^,]*,)')
eol=re.compile('\015?\012')
CSVError='csv.CSVError'
def csv2list(line):
line, n = eol.subn(',', line)
if not n: raise CSVError, 'record not complete'
ret=[]
while 1:
m=csv_entry.match(line)
if not m: break
len = m.end()
field, line = line[:len], line[len:]
if field[0]=='"': field=field[1:-2]
else: field=field[:-1]
ret.append(field)
return ret
----------------- end python code -----------------------------
Thanks in advance.
--
Moshe Zadka <mzadka at geocities.com>.
QOTD: What fun to me! I'm not signing permanent.
More information about the Python-list
mailing list