How to export dictionary as CSV using Python? -
i having problems exporting items in dictionary csv. can export 'name' not 'images' (the image url).
this example of part of 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 mistake naming pointed out. seems work after updating it).
and code have written (which works 'name' not 'picture'):
import csv test = [] document in new: event_obj = {} # name event_obj['name'] = document['name'] # images event_obj['picture'] = document['picture'] test.append(event_obj) # create csv file open('eventbrite_events.csv', 'w', newline='') csvfile: fields = ['name', 'picture'] writer = csv.dictwriter(csvfile, fieldnames=fields) writer.writeheader() x in test: writer.writerow(x) print(csvfile)
your new
list contains dictionaries key picture
trying access 1 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 = [] document in new: event_obj = {} # name event_obj['name'] = document['name'] # images event_obj['images'] = document['picture'] test.append(event_obj) # create csv file open('eventbrite_events.csv', 'w', newline='') csvfile: fields = ['name', 'images'] writer = csv.dictwriter(csvfile, fieldnames=fields) writer.writeheader() writer.writerows(test)
you make use of writerows()
write rows in 1 go.
this give csv file 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 avoid building test
follows alternative way rename 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 open('eventbrite_events.csv', 'w', newline='') csvfile: fields = ['name', 'images'] writer = csv.dictwriter(csvfile, fieldnames=fields) writer.writeheader() row in new: row['images'] = row.pop('picture') writer.writerow(row)
Comments
Post a Comment