'How to export dictionary as CSV using Python?
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)
Solution 1:[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)
Solution 2:[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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | stamaimer |