如何将这种格式的CSV文件转换为json文件?



CSV文件看起来是这样的,我有id和标签。但是有些标签可能有多个值,每个值占一列,所以有些标签可能有多个列。

id,tags
403744,[""]
403745,["phsecurity"]
403750,["unit-testing","testing","boolean"]
403757,[""]
403759,["object-oriented","architectural-patterns"]

我尝试使用这个python代码来转换:

import csv
import json

# Function to convert a CSV to JSON
# Takes the file paths as arguments
def make_json(csvFilePath, jsonFilePath):
# create a dictionary
data = {}
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
# Convert each row into a dictionary
# and add it to data
for rows in csvReader:
# Assuming a column named 'No' to
# be the primary key
key = rows['id']
data[key] = rows
# Open a json writer, and use the json.dumps()
# function to dump data
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
# Driver Code

# Decide the two file paths according to your
# computer system
csvFilePath = r'aaa.CSV'
jsonFilePath = r'bbb.json'
# Call the make_json function
make_json(csvFilePath, jsonFilePath)

但是它给出了这样的格式:{"403744":{"id"403744","tags"["]";},"403745":{"id"403745","tags"["phsecurity";},"403750":{"id"403750","tags"("unit-testing""null"("testing","布尔]";]}

json格式不正确。比如我想加上"测试"one_answers";boolean"也在标签中。你还可以看到,它写着"["或"]"价值也是如此。谁知道怎么修理它?谢谢

最好的方法是更改输入文件的生成方式。但如果不可能,这个脚本将尝试解析它并保存Json文件:

import json
from ast import literal_eval
data = []
with open("input.csv", "r") as f_in:
for line in map(str.strip, f_in):
if line == "":
continue
line = list(map(str.strip, line.split(",", maxsplit=1)))
data.append(line)
# skip header:
data = data[1:]

data = {key: {"id": key, "tags": literal_eval(tags)} for key, tags in data}
print(data)
with open("output.json", "w") as f_out:
json.dump(data, f_out, indent=4)

将创建output.json:

{
"403744": {
"id": "403744",
"tags": [
""
]
},
"403745": {
"id": "403745",
"tags": [
"phsecurity"
]
},
"403750": {
"id": "403750",
"tags": [
"unit-testing",
"testing",
"boolean"
]
},
"403757": {
"id": "403757",
"tags": [
""
]
},
"403759": {
"id": "403759",
"tags": [
"object-oriented",
"architectural-patterns"
]
}
}

相关内容

  • 没有找到相关文章

最新更新