Reading/Writing out a dictionary to csv file in python

后端 未结 3 671
灰色年华
灰色年华 2021-02-05 17:14

Pretty new to python, and the documentation for csv files is a bit confusing.

I have a dictionary that looks like the following:

key1: (value1, value2)

         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 17:38

    I would use pandas, it can be done in one line:

    import pandas as pd
    
    dic = {'key1':['v1','v2'], 'key2':['vv','gg']}
    
    pd.DataFrame(dic).T.reset_index().to_csv('myfile.csv', header=False, index=False)
    

提交回复
热议问题