Mercurial > p > roundup > code
diff roundup/rcsv.py @ 1767:fdaa0b751355
python2.3 CSV support, also missing thankyou in index.txt :)
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 28 Aug 2003 04:46:54 +0000 |
| parents | |
| children | 0afdf96ea9d7 3555941caea0 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/roundup/rcsv.py Thu Aug 28 04:46:54 2003 +0000 @@ -0,0 +1,78 @@ +""" +Supplies a Python-2.3 Object Craft csv module work-alike to the extent +needed by Roundup using the Python 2.3 csv module. + +""" + +from roundup.i18n import _ +error = """Sorry, you need a module compatible with the csv module. +Either upgrade your Python to 2.3 or later, or get and install +the csv module from: +http://www.object-craft.com.au/projects/csv/ + +These two csv modules are different but Roundup can use either. +""" +try: + import csv + try: + reader = csv.reader + writer = csv.writer + excel = csv.excel + error = '' + except AttributeError: + # fake it all up using the Object-Craft CSV module + class excel: + pass + if hasattr(csv, 'parser'): + error = '' + def reader(fileobj, dialect=excel): + # note real readers take an iterable but 2.1 doesn't + # support iterable access to file objects. + result = [] + p = csv.parser(field_sep=dialect.delimiter) + + while 1: + line = fileobj.readline() + if not line: break + + # parse lines until we get a complete entry + while 1: + fields = p.parse(line) + if fields: break + line = fileobj.readline() + if not line: + raise ValueError, "Unexpected EOF during CSV parse" + result.append(fields) + return result + class writer: + def __init__(self, fileobj, dialect=excel): + self.fileobj = fileobj + self.p = csv.parser(field_sep = dialect.delimiter) + def writerow(self, fields): + print >>self.fileobj, self.p.join(fields) + def writerows(self, rows): + for fields in rows: + print >>self.fileobj, self.p.join(fields) + +except ImportError: + class excel: + pass + +class colon_separated(excel): + delimiter = ':' +class comma_separated(excel): + delimiter = ',' + + +if __name__ == "__main__": + f=open('testme.txt', 'r') + r = reader(f, colon_separated) + remember = [] + for record in r: + print record + remember.append(record) + f.close() + import sys + w = writer(sys.stdout, colon_separated) + w.writerows(remember) +
