如何修改这个python脚本以获得相同的json文件而不使用highted部分?

2024-09-30 06:25:33 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我正在尝试从csv文件导入由以下python脚本创建的json文件

import csv, json

csvFilePath ='USvideos.csv'
jsonFilePath = 'USvideos.json'
data = {}

with open(csvFilePath, encoding = 'utf8') as csvFile:
    csvReader = csv.DictReader(csvFile)
    for csvRow in csvReader:
        video_id = csvRow['video_id']
        data[video_id] = csvRow

with open(jsonFilePath, 'w') as jsonFile:
    jsonFile.write(json.dumps(data, indent=4))

问题语句问题是,我需要通过修改python脚本来获得一个不带括号部分的json文件

("2kyS6SvSYSE": ) {
    "video_id": "2kyS6SvSYSE",
    "trending_date": "17.20.11",
    "title": "WE WANT TO TALK ABOUT OUR MARRIAGE"
},
("1ZAPwfrtAFY":) {
    "video_id": "1ZAPwfrtAFY",
    "trending_date": "17.20.11"
}

解决的目的 我需要解决这个问题,因为我想在MongoDB中适当地导入数据


Tags: 文件csvcsvfile脚本idjsondataas
1条回答
网友
1楼 · 发布于 2024-09-30 06:25:33

猜测您需要的输出JSON格式,但是您能尝试一下吗

import csv, json

csvFilePath ='USvideos.csv'
jsonFilePath = 'USvideos.json'
data = []

with open(csvFilePath, encoding = 'utf8') as csvFile:
    csvReader = csv.DictReader(csvFile)
    for csvRow in csvReader:
        data.append(csvRow)

with open(jsonFilePath, 'w') as jsonFile:
    jsonFile.write(json.dumps(data, indent=4))

相关问题 更多 >

    热门问题