How to export dictionary as CSV using Python?

こ雲淡風輕ζ 提交于 2019-12-08 14:12:10

问题


I am having problems exporting certain items in a dictionary to CSV. I can export 'name' but not 'images' (the image URL).

This is an example of part of my dictionary:

new = [{ "name" : "peter", "picture" : "https://img.evbuc.com/https%3A%2F%2Fcdn.evbuc.com%2Fimages%2F33500665%2F25911657759%2F1%2Foriginal.jpg?h=200&w=450&rect=0%2C581%2C6000%2C3000&s=bfaa2901b8c906a66c51563d15c6df12"},
{"name" : "jim" , "picture" : "https://img.evbuc.com/https%3A%2F%2Fcdn.evbuc.com%2Fimages%2F32935536%2F10115879927%2F1%2Foriginal.jpg?h=200&w=450&rect=0%2C40%2C624%2C312&s=c67e995e83234ab460707ac21f3541f8"}]

EDIT (I made a mistake with the naming as was what pointed out. It seems to work now after updating it).

And this is the code I have written (which works for 'name' but not 'picture'):

import csv

test = []

for document in new:
    event_obj = {}

    # Get name
    event_obj['name'] = document['name']

    # Get images
    event_obj['picture'] = document['picture']

    test.append(event_obj)

# Create CSV file
with open('Eventbrite_events.csv', 'w', newline='') as csvfile:
     fields = ['name', 'picture']
     writer = csv.DictWriter(csvfile, fieldnames=fields)
     writer.writeheader()
     for x in test:
         writer.writerow(x)
print(csvfile)

回答1:


Your new list contains dictionaries with the key picture but you were trying to access one called images.

import csv

new = [
    { "name" : "peter", "picture" : "https://img.evbuc.com/https%3A%2F%2Fcdn.evbuc.com%2Fimages%2F33500665%2F25911657759%2F1%2Foriginal.jpg?h=200&w=450&rect=0%2C581%2C6000%2C3000&s=bfaa2901b8c906a66c51563d15c6df12"},
    {"name" : "jim" , "picture" : "https://img.evbuc.com/https%3A%2F%2Fcdn.evbuc.com%2Fimages%2F32935536%2F10115879927%2F1%2Foriginal.jpg?h=200&w=450&rect=0%2C40%2C624%2C312&s=c67e995e83234ab460707ac21f3541f8"}]

test = []

for document in new:
    event_obj = {}

    # Get name
    event_obj['name'] = document['name']

    # Get images
    event_obj['images'] = document['picture']

    test.append(event_obj)

# Create CSV file
with open('Eventbrite_events.csv', 'w', newline='') as csvfile:
    fields = ['name', 'images']
    writer = csv.DictWriter(csvfile, fieldnames=fields)
    writer.writeheader()
    writer.writerows(test)

You could also make use of writerows() to write all the rows in one go.

This would give you a CSV file as follows:

name,images
peter,https://img.evbuc.com/https%3A%2F%2Fcdn.evbuc.com%2Fimages%2F33500665%2F25911657759%2F1%2Foriginal.jpg?h=200&w=450&rect=0%2C581%2C6000%2C3000&s=bfaa2901b8c906a66c51563d15c6df12
jim,https://img.evbuc.com/https%3A%2F%2Fcdn.evbuc.com%2Fimages%2F32935536%2F10115879927%2F1%2Foriginal.jpg?h=200&w=450&rect=0%2C40%2C624%2C312&s=c67e995e83234ab460707ac21f3541f8

You could also avoid building test as follows as an alternative way to rename your entry:

import csv

new = [
    {"name" : "peter", "picture" : "https://img.evbuc.com/https%3A%2F%2Fcdn.evbuc.com%2Fimages%2F33500665%2F25911657759%2F1%2Foriginal.jpg?h=200&w=450&rect=0%2C581%2C6000%2C3000&s=bfaa2901b8c906a66c51563d15c6df12"},
    {"name" : "jim" , "picture" : "https://img.evbuc.com/https%3A%2F%2Fcdn.evbuc.com%2Fimages%2F32935536%2F10115879927%2F1%2Foriginal.jpg?h=200&w=450&rect=0%2C40%2C624%2C312&s=c67e995e83234ab460707ac21f3541f8"}]

# Create CSV file
with open('Eventbrite_events.csv', 'w', newline='') as csvfile:
    fields = ['name', 'images']

    writer = csv.DictWriter(csvfile, fieldnames=fields)
    writer.writeheader()

    for row in new:
        row['images'] = row.pop('picture')
        writer.writerow(row)



回答2:


Pandas is more suitable for your question.

import pandas as pd

df = pf.DataFrame(list_of_dict)

df.columns = ["name", "image or picture"]

df.to_csv("test.csv", index=0)


来源:https://stackoverflow.com/questions/45690380/how-to-export-dictionary-as-csv-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!