【发布时间】:2021-07-23 23:44:00
【问题描述】:
我正在尝试将我的 CSV 电子邮件列表转换为 JSON 格式以通过 API 群发电子邮件。到目前为止,这是我的代码,但输出有问题。我的 VS 代码编辑器上没有任何输出。
import csv
import json
def make_json(csvFilePath, jsonFilePath):
data = {}
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
for rows in csvReader:
key = rows['No']
data[key] = rows
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
csvFilePath = r'/data/csv-leads.csv'
jsonFilePath = r'Names.json'
make_json(csvFilePath, jsonFilePath)
这是我想要的 JSON 格式
{
"EmailAddress": "hello@youngstowncoffeeseattle.com",
"Name": "Youngstown Coffee",
"ConsentToTrack": "Yes"
},
这是我的 CSV 列表
No,EmailAddress,ConsentToTrack
Zylberschtein's Delicatessen & Bakery,catering@zylberschtein.com,Yes
Youngstown Coffee,hello@youngstowncoffeeseattle.com,Yes
【问题讨论】:
-
请不要在您的问题中添加指向图片的链接。只需包含 CSV 文件的示例,格式为代码示例(就像您的 Python 代码一样)。
-
哦,明白了!更新了示例 CSV 列表