[Tutor] Malformed CSV

Kent Johnson kent37 at tds.net
Fri Dec 2 16:46:11 CET 2005

Jan Eden wrote:
> I guess I need to notify the engineer responsible for the CSV output
> and have the quoting corrected.

If that is possible it is a much better solution. I hate to hack around bad data - much better to correct the source of the data if possible.

In fact you may have little choice if you want to use the CSV module - for example what if the first field has commas and quotes? Could you have a line like this?
""hotel", "budapest", "billig"","1","0","0"

Alternately you could just write your own parser, if the data is very regular this may not be hard:

for line in data.splitlines():
    fields = line.rsplit(',', 3)
    fields = [ field[1:-1] for field in fields ]
    print fields

BTW do you really have doubled quotes in all the hotel fields, or just the one with quotes? The data in your original post is inconsistent.


More information about the Tutor mailing list