简体   繁体   中英

How to save list output as csv file

Using python 3.5 I have a output file in the form of :

result = 
[{'alt': '',
  'company_id': 309,
  'confidence': 0,
  'person_id': 17591,
  'person_image_url': '',
  'person_name': 'abc'},
 {'alt': '',
  'company_id': 309,
  'confidence': 0,
  'person_id': 20298,
  'person_image_url': '',
  'person_name': 'def'},
 {'alt': '',
  'company_id': 309,
  'confidence': 0,
  'person_id': 20301,
  'person_image_url': '',
  'person_name': 'ghi'}]

How do I save this as a CSV file with the format below:

alt company_id confidence person_id person_image_url person_name
      309          0        17591         ''            abc   
      309          0        20298         ''            def
      309          0        20301         ''            ghi
import pandas as pd
df = pd.DataFrame(result)
df.to_csv('result.csv', index=False, sep='\t')

You could use the package pandas :

import pandas as pd
df = pd.DataFrame(result)
print(df)
df.to_csv('result.csv', index=False, sep=';')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM