Python Read csv files in a column?

后端 未结 3 1621
挽巷
挽巷 2021-01-25 13:22

Is it possible to read csv files in columns instead of rows in python?

e.g. if i have a csv file like this:

a b c
1 0 1
1 4 1

How woul

3条回答
  •  清酒与你
    2021-01-25 13:48

    use zip(*reader).

    some.csv

    a b c 
    1 0 1 
    1 4 1
    

    scrip.py

    import csv 
    with open('some.csv', 'rb') as f:
        reader = csv.reader(f, delimiter=' ')
        print zip(*reader)
    

    output:

    [('a', '1', '1'), ('b', '0', '4'), ('c', '1', '1')]
    

提交回复
热议问题