From: moshez at math.huji.ac.il (Moshe Zadka)
Date: Sat, 24 Apr 1999 20:41:16 +0300
Subject: CSV Module
Message-ID: <Pine.SUN.3.95-heb-2.07.990424203855.9433B-100000@sunset.ma.huji.ac.il>
Content-Length: 1087
X-UID: 827                                                  

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.





