Is anyone happy with csv module?

Istvan Albert istvan.albert at gmail.com
Tue Dec 11 21:22:33 EST 2007


On Dec 11, 2:14 pm, "massimo s." <deviceran... at gmail.com> wrote:

> dislike more is that it seems working by *rows* instead than by
> *columns*.

you can easily transpose the data to get your columns, for a data file
that looks like this:

---- data.txt ----
A,B,C
1,2,3
10,20,30
100,200,300

do the following:

--------------------
import csv
reader = csv.reader( file('data.txt', 'U') )
rows = list(reader)

print rows

cols = zip(*rows)

print cols[0]
print cols[1]
print cols[2]

this will print:

---------- Python ----------

[['A', 'B', 'C'], ['1', '2', '3'], ['10', '20', '30'], ['100', '200',
'300']]
('A', '1', '10', '100')
('B', '2', '20', '200')
('C', '3', '30', '300')




More information about the Python-list mailing list