Python Dictionary to CSV

后端 未结 6 793
情深已故
情深已故 2020-11-27 16:09

I have written code to read a CSV into a python dictionary, which works fine. I\'m trying to get the dictionary back to a CSV. I have written the following:

         


        
6条回答
  •  猫巷女王i
    2020-11-27 16:49

    Sample data:

    mydict = [{"col1": 1000, "col2": 2000}, {"col1": 3000, "col2": 4000}]
    

    One-liner for converting a list of dicts to CSV, using pandas:

    import pandas as pd
    
    pd.DataFrame(mydict).to_csv('out.csv', index=False)
    

    Results:

    col1,col2
    1000,2000
    3000,4000
    

提交回复
热议问题