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

How to export dictionary as CSV using Python?

我在将字典中的某些项目导出到 CSV 时遇到问题。我可以导出 'name' 但不能导出 'images'(图像 URL)。

这是我字典的一部分示例:

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"}]

编辑(正如指出的那样,我的命名有误。更新后现在似乎可以使用了)。

这是我编写的代码(适用于 'name' 但不适用于 '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)

Pandas更适合你的问题

import pandas as pd

df = pf.DataFrame(list_of_dict)

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

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

您的 new 列表包含关键字为 picture 的词典,但您试图访问一个名为 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()一次性写入所有行。

这将为您提供如下 CSV 文件:

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 作为重命名条目的替代方法:

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)