CSV module: incorrectly parsed file.

7stud bbxx789_05ss at yahoo.com
Sun Feb 17 23:11:16 EST 2008


On Feb 17, 7:09 pm, Christopher Barrington-Leigh
<christophe... at gmail.com> wrote:
> Here is a file "test.csv"
> number,name,description,value
> 1,"wer","tape 2"",5
> 1,vvv,"hoohaa",2
>
> I want to convert it to tab-separated without those silly quotes. Note
> in the second line that a field is 'tape 2"' , ie two inches: there is
> a double quote in the string.
>
> When I use csv module to read this:
>
> import sys
> outf=open(sys.argv[1]+'.tsv','wt')
> import csv
> reader=csv.reader(open(sys.argv[1], "rb"))
> for row in reader:
>     outf.write('\t'.join([rr.strip() for rr in row]) +'\n')
>
> it mangles it, messing up the double double-quote.
> Can anyone help me? How do I use CSV to get it right?
> Tjhanks!
> c


Try this:

infile = open('data.txt')
outfile = open('outfile.txt', 'w')

for line in infile:
    pieces = line.strip().split(',')

    data = []
    for piece in pieces:
        if piece[0] == '"':
            data.append(piece[1:-2])
        else:
            data.append(piece)

    out_line = '%s\n' % '\t'.join(data)
    outfile.write(out_line)



More information about the Python-list mailing list