如何将 CSV 文件转换为多行 JSON? [英] How to convert CSV file to multiline JSON?

查看:42
本文介绍了如何将 CSV 文件转换为多行 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,非常简单的东西...

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)

声明一些字段名,读取器使用CSV读取文件,并使用字段名将文件转储为JSON格式.问题来了...

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...

CSV 文件中的每条记录都位于不同的行上.我希望 JSON 输出是相同的方式.问题是它把它全部倾倒在一条巨大的长线上.

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.

我尝试过使用类似 for line in csvfile: 的东西,然后使用 reader = csv.DictReader( line, fieldnames) 在它下面运行我的代码,它循环遍历每一行,但它在一行上处理整个文件,然后在另一行上循环整个文件......继续直到它用完行.

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?

澄清一下,目前我有:(第 1 行的每条记录)

[{"FirstName":"John","LastName":"Doe","IDNumber":"123","Message":"None"},{"FirstName":"George","LastName":"Washington","IDNumber":"001","Message":"Something"}]

我在找什么:(2 行 2 条记录)

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.

一些示例输入.

"John","Doe","001","Message1"
"George","Washington","002","Message2"

推荐答案

您想要的输出的问题是它不是有效的 json 文档;这是一个json文档流

The problem with your desired output is that it is not valid json document,; it's a stream of json documents!

没关系,如果它是您需要的,但这意味着对于您想要在输出中的每个文档,您必须调用 json.dumps.

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.

由于您要分隔文档的换行符未包含在这些文档中,因此您需要自己提供换行符.所以我们只需要将循环从对 json.dump 的调用中拉出来,并为每个写入的文档插入换行符.

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('
')

这篇关于如何将 CSV 文件转换为多行 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆