我有一个delim数据文件,如下所示
DAYPART_ID|NAME|LABEL|START_TIME|END_TIME|WEEKEDAYS|STYLE|DAYPART_SET_ID|ORDER
1|Early AM|6:00 am - 9:00 am|6|9|12345|gold|1|01
2|Daytime|9:00 am - 4:00 pm|9|16|12345|red|1|02
我需要将它转换为以下类型的Json列表文件
[
{
"STYLE": "gold",
"NAME": "Early AM",
"START_TIME": 6,
"DAYPART_SET_ID": 1,
"LABEL": "6:00 am - 9:00 am",
"DAYPART_ID": 1,
"END_TIME": 9,
"ORDER": 01,
"WEEKEDAYS": 12345
},
{
"STYLE": "red",
"NAME": "Daytime",
"START_TIME": 9,
"DAYPART_SET_ID": 1,
"LABEL": "9:00 am - 4:00 pm",
"DAYPART_ID": 2,
"END_TIME": 16,
"ORDER": 02,
"WEEKEDAYS": 12345
}
]
所以虽然它是一个JSON文件,但它有点修改,如数字字段没有引号,我们在文件中有额外的第三个括号,每个记录之间有一个逗号,除了有一个结束花括号。
我写了一个如下编码
import csv
import json
csv.register_dialect('pipe', delimiter='|', quoting=csv.QUOTE_NONE)
with open('Infile', "r") as csvfile:
with open(outtfile, 'w') as outfile:
for row in csv.DictReader(csvfile, dialect='pipe'):
data= row
json.dump(data, outfile, sort_keys = False, indent = 0,ensure_ascii=True)
但它没有给我确切的结果。我打算。有人可以帮忙吗?
答案 0 :(得分:1)
您正在做的是将每行转储到目标文件。这些对象不知道是否在列表中,因此输出文件中缺少json文件的列表语法。问题的解决方案是读取要列出的所有对象,然后转储列表本身。
对于数字 - 只需列出期望类型为int的所有列,并在添加到objects
列表之前对其进行转换。
import csv
import json
csv.register_dialect('pipe', delimiter='|', quoting=csv.QUOTE_NONE)
numeric_columns = ['START_TIME', 'END_TIME', 'WEEKEDAYS', 'DAYPART_SET_ID', 'DAYPART_ID']
objects = []
with open('infile', "r") as csvfile:
for o in csv.DictReader(csvfile, dialect='pipe'):
for k in numeric_columns:
o[k] = int(o[k])
objects.append(o)
with open('outfile', 'w') as dst:
json.dump(objects, dst, indent=2)