如何使用Python将字典导出为CSV? [英] How to export dictionary as CSV using Python?

查看:1805
本文介绍了如何使用Python将字典导出为CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将字典中的某些项目导出到CSV时遇到问题.我可以导出名称",但不能导出图像"(图像URL).

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)

推荐答案

您的new列表包含键为picture的词典,但是您正在尝试访问一个名为images的词典.

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)

您还可以使用writerows()一次性写入所有行.

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

这将为您提供一个CSV文件,如下所示:

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


您还可以避免如下构建test作为重命名条目的另一种方法:


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)

这篇关于如何使用Python将字典导出为CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆