您所需输出的问题是它不是有效的json文档;这 是json文档流 !
没关系,如果您需要的话,但这意味着对于输出中想要的每个文档,您都必须调用
json.dumps。
由于要分隔文档的换行符不包含在这些文档中,因此您需要自己提供它。因此,我们只需要从对json.dump的调用中拉出循环,并为每个编写的文档插入换行符即可。
import csvimport jsoncsvfile = 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')


