python导出成csv_如何使用Python将字典导出为CSV?

1586010002-jmsa.png

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)

解决方案

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值