Mercurial > p > roundup > code
comparison roundup/rcsv.py @ 1862:0afdf96ea9d7
Add a wrapper around the two different reader() functions...
which removes all empty lines, as both csv parsers barf on them [SF#821364].
| author | Johannes Gijsbers <jlgijsbers@users.sourceforge.net> |
|---|---|
| date | Fri, 24 Oct 2003 16:29:17 +0000 |
| parents | fdaa0b751355 |
| children | fc52d57c6c3e |
comparison
equal
deleted
inserted
replaced
| 1861:6c3357448c7d | 1862:0afdf96ea9d7 |
|---|---|
| 3 needed by Roundup using the Python 2.3 csv module. | 3 needed by Roundup using the Python 2.3 csv module. |
| 4 | 4 |
| 5 """ | 5 """ |
| 6 | 6 |
| 7 from roundup.i18n import _ | 7 from roundup.i18n import _ |
| 8 from cStringIO import StringIO | |
| 8 error = """Sorry, you need a module compatible with the csv module. | 9 error = """Sorry, you need a module compatible with the csv module. |
| 9 Either upgrade your Python to 2.3 or later, or get and install | 10 Either upgrade your Python to 2.3 or later, or get and install |
| 10 the csv module from: | 11 the csv module from: |
| 11 http://www.object-craft.com.au/projects/csv/ | 12 http://www.object-craft.com.au/projects/csv/ |
| 12 | 13 |
| 13 These two csv modules are different but Roundup can use either. | 14 These two csv modules are different but Roundup can use either. |
| 14 """ | 15 """ |
| 15 try: | 16 try: |
| 16 import csv | 17 import csv |
| 17 try: | 18 try: |
| 18 reader = csv.reader | 19 _reader = csv.reader |
| 19 writer = csv.writer | 20 writer = csv.writer |
| 20 excel = csv.excel | 21 excel = csv.excel |
| 21 error = '' | 22 error = '' |
| 22 except AttributeError: | 23 except AttributeError: |
| 23 # fake it all up using the Object-Craft CSV module | 24 # fake it all up using the Object-Craft CSV module |
| 24 class excel: | 25 class excel: |
| 25 pass | 26 pass |
| 26 if hasattr(csv, 'parser'): | 27 if hasattr(csv, 'parser'): |
| 27 error = '' | 28 error = '' |
| 28 def reader(fileobj, dialect=excel): | 29 def _reader(fileobj, dialect=excel): |
| 29 # note real readers take an iterable but 2.1 doesn't | 30 # note real readers take an iterable but 2.1 doesn't |
| 30 # support iterable access to file objects. | 31 # support iterable access to file objects. |
| 31 result = [] | 32 result = [] |
| 32 p = csv.parser(field_sep=dialect.delimiter) | 33 p = csv.parser(field_sep=dialect.delimiter) |
| 33 | 34 |
| 61 class colon_separated(excel): | 62 class colon_separated(excel): |
| 62 delimiter = ':' | 63 delimiter = ':' |
| 63 class comma_separated(excel): | 64 class comma_separated(excel): |
| 64 delimiter = ',' | 65 delimiter = ',' |
| 65 | 66 |
| 67 def reader(fileobject, dialect=excel): | |
| 68 csv_lines = [line for line in fileobject.readlines() if line.strip()] | |
| 69 return _reader(StringIO(''.join(csv_lines)), dialect) | |
| 66 | 70 |
| 67 if __name__ == "__main__": | 71 if __name__ == "__main__": |
| 68 f=open('testme.txt', 'r') | 72 f=open('testme.txt', 'r') |
| 69 r = reader(f, colon_separated) | 73 r = reader(f, colon_separated) |
| 70 remember = [] | 74 remember = [] |
