asyndrige
asyndrige

Reputation: 592

Parsing unicode csv with unicodecsv module

Parsing csv output of console command with Python 2.7. Standart csv module did not work properly, so I`ve found unicodecsv module. The iteration outputs strange results (empty lists or strings separated to chars), but I expect to see list with values for each row:

import unicodecsv
csv_parsed = unicodecsv.reader(utf8_csv_string, delimiter=',')

for row in csv_parsed:
    print row

>[u'0']
>[]
>[u'']
>[u'', u'']
>[u'', u'']
>[u'', u'']
>[u'19102.00']
>[u'', u'']
>[u'2']
>[u'1']
>[u'2']
>[u'', u'']
>[u'0']
>[]

Where is the mistake?

PS: Using Python 3 is not an option.

Upvotes: 0

Views: 545

Answers (1)

John
John

Reputation: 13699

According to the documentation unicodecsv expects a bytestream. So

with open('file.txt', 'rb') as f:
    data = unicodecsv.reader(f, delimiter=',')

should work. Actually if you look at the tests on github you can see that is how the author of the library uses it.

Upvotes: 1

Related Questions