Creating a dictionary from a csv file?

后端 未结 16 2084
北荒
北荒 2020-11-22 06:13

I am trying to create a dictionary from a csv file. The first column of the csv file contains unique keys and the second column contains values. Each row of the csv file rep

16条回答
  •  悲&欢浪女
    2020-11-22 06:35

    with pandas, it is much easier, for example. assuming you have the following data as CSV and let's call it test.txt / test.csv (you know CSV is a sort of text file )

    a,b,c,d
    1,2,3,4
    5,6,7,8
    

    now using pandas

    import pandas as pd
    df = pd.read_csv("./text.txt")
    df_to_doct = df.to_dict()
    

    for each row, it would be

    df.to_dict(orient='records')
    

    and that's it.

提交回复
热议问题