Reading rows from a CSV file in Python

前端 未结 10 2371
慢半拍i
慢半拍i 2020-11-30 23:58

I have a CSV file, here is a sample of what it looks like:

Year:  Dec: Jan:
1      50   60
2      25   50
3      30   30
4      40   20
5      10   10
         


        
10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 00:29

    Reading it columnwise is harder?

    Anyway this reads the line and stores the values in a list:

    for line in open("csvfile.csv"):
        csv_row = line.split() #returns a list ["1","50","60"]
    

    Modern solution:

    # pip install pandas
    import pandas as pd 
    df = pd.read_table("csvfile.csv", sep=" ")
    

提交回复
热议问题