Python CSV到JSON [英] Python CSV to JSON
问题描述
这是我的代码,真的很简单的东西...
import csv
import json
csvfile = open('file.csv','r')
jsonfile = open('file.json','w')
fieldnames = lastName,IDNumber,Message)
reader = csv.DictReader(csvfile,fieldnames)
out = json.dumps(阅读器中的行)
jsonfile。写(out)
声明一些字段名,读者使用CSV读取文件,名称将文件转储为JSON格式。这是问题...
CSV文件中的每个记录都位于不同的行。我想要的JSON输出是相同的方式。问题是它把它都放在一个巨大的长线上。
我试过在csvfile中使用类似的行:
,然后在下面运行我的代码 reader = csv.DictReader(line,fieldnames)
循环遍历每一行,但它在一行上完成整个文件,然后循环遍历整个文件在另一行...继续
编辑:若要澄清,现在我有:(第1行的每个记录)
[{FirstName:John LastName:Doe,IDNumber:123,Message:None},{FirstName:George,LastName:Washington,IDNumber:001 Message:Something}]
我在找什么:
{FirstName:John,LastName:Doe,IDNumber:123 ,Message:None}
{FirstName:George,LastName:Washington,IDNumber:001,Message:Something}
不是每个单独的字段缩进/在单独的行,但每个记录都在自己的行。
一些示例输入。
John,Doe,001 Message1
George,Washington,002,Message2
你想要的输出的问题是它是无效的json文档,;它是一个json文档流!
没有问题,如果你需要它,但这意味着对于每个文档你想在你的输出,你必须调用 json.dumps
。 由于不想包含换行符在这些文档中,你自己就可以提供它了。因此,我们只需要从调用json.dump中拉出循环,并为每个写入的文档插入换行符。
import csv
import json
csvfile = open('file.csv','r')
jsonfile = open('file.json','w')
fieldnames =(FirstName,LastName,IDNumber,Message)
reader = csv.DictReader(csvfile,fieldnames)
读取行:
json.dump(row,jsonfile)
jsonfile.write('\\\
')
Here's my code, really simple stuff...
import csv
import json
csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')
fieldnames = ("FirstName","LastName","IDNumber","Message")
reader = csv.DictReader( csvfile, fieldnames)
out = json.dumps( [ row for row in reader ] )
jsonfile.write(out)
Declare some field names, the reader uses CSV to read the file, and the filed names to dump the file to a JSON format. Here's the problem...
Each record in the CSV file is on a different row. I want the JSON output to be the same way. The problem is it dumps it all on one giant, long line.
I've tried using something like for line in csvfile:
and then running my code below that with reader = csv.DictReader( line, fieldnames)
which loops through each line, but it does the entire file on one line, then loops through the entire file on another line... continues until it runs out of lines.
Any suggestions for correcting this?
Edit: To clarify, currently I have: (every record on line 1)
[{"FirstName":"John","LastName":"Doe","IDNumber":"123","Message":"None"},{"FirstName":"George","LastName":"Washington","IDNumber":"001","Message":"Something"}]
What I'm looking for: (2 records on 2 lines)
{"FirstName":"John","LastName":"Doe","IDNumber":"123","Message":"None"}
{"FirstName":"George","LastName":"Washington","IDNumber":"001","Message":"Something"}
Not each individual field indented/on a separate line, but each record on it's own line.
Some sample input.
"John","Doe","001","Message1"
"George","Washington","002","Message2"
The problem with your desired output is that it is not valid json document,; it's a stream of json documents!
That's okay, if its what you need, but that means that for each document you want in your output, you'll have to call json.dumps
.
Since the newline you want separating your documents is not contained in those documents, you're on the hook for supplying it yourself. So we just need to pull the loop out of the call to json.dump and interpose newlines for each document written.
import csv
import json
csvfile = open('file.csv', 'r')
jsonfile = open('file.json', 'w')
fieldnames = ("FirstName","LastName","IDNumber","Message")
reader = csv.DictReader( csvfile, fieldnames)
for row in reader:
json.dump(row, jsonfile)
jsonfile.write('\n')
这篇关于Python CSV到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!