【问题标题】:Python and CSV: Reading JSON from CSVPython 和 CSV:从 CSV 读取 JSON
【发布时间】:2015-05-15 13:26:20
【问题描述】:

我有一个从 CSV 文件读取并将内容转储到所选文件的函数。此 csv 文件的内容是一列,其中每行包含一系列 JSON 对象,如下所示(使用 jsonlint 验证):

 [{"className": "Merchant", "__type": "Pointer", "objectId": "S8IGOwBn8Y"}, {"className": "Merchant", "__type": "Pointer", "objectId": "psNnxwFVmv"}, {"className": "Merchant", "__type": "Pointer", "objectId": "IHcC9ikKBJ"}, {"className": "Merchant", "__type": "Pointer", "objectId": "RVprbh5nWx"}, {"className": "Merchant", "__type": "Pointer", "objectId": "47Zjn9RRov"}, {"className": "Merchant", "__type": "Pointer", "objectId": "CoGtlmGzyo"}, {"className": "Merchant", "__type": "Pointer", "objectId": "yJHn9dBCIT"}, {"className": "Merchant", "__type": "Pointer", "objectId": "nEOY9RPRD4"}]

稍微概念化一下,这就是我想要的:

csv file ---> function that I wrote ---> something.json

在这种情况下,我使用 test.csv 作为输入,使用 test.txt 作为输出。对于第一个 json 流,它似乎工作正常,但有一个怪癖:

第一个 JSON 对象出来就好了:

[

{"className": "Merchant", "__type": "Pointer", "objectId": "S8IGOwBn8Y"}, 
{"className": "Merchant", "__type": "Pointer", "objectId": "psNnxwFVmv"}, 
{"className": "Merchant", "__type": "Pointer", "objectId": "IHcC9ikKBJ"}],

但在那之后,他们开始像这样出来:

"[{""className"": ""Merchant"", ""__type"": ""Pointer"", ""objectId"": ""S8IGOwBn8Y""}, 
{""className"": ""Merchant"", ""__type"": ""Pointer"", ""objectId"":  ""psNnxwFVmv""}, 
{""className"": ""Merchant"", ""__type"": ""Pointer"", ""objectId"": ""IHcC9ikKBJ""}, 
{""className"": ""Merchant"", ""__type"": ""Pointer"", ""objectId"": ""RVprbh5nWx""}, 
{""className"": ""Merchant"", ""__type"": ""Pointer"", ""objectId"": ""47Zjn9RRov""}, 
{""className"": ""Merchant"", ""__type"": ""Pointer"", ""objectId"": ""CoGtlmGzyo""}, 
{""className"": ""Merchant"", ""__type"": ""Pointer"", ""objectId"": ""yJHn9dBCIT""}, 
 {""className"": ""Merchant"", ""__type"": ""Pointer"", ""objectId"": ""nEOY9RPRD4""}', 
'\n[{""className"": ""Merchant"", ""__type"": ""Pointer"", ""objectId"": ""tNgLB9dobR""}, 

这是执行此操作的函数:

def read_csv(thefile):

    f = open('test.txt','w')
    with open(thefile, 'rb') as csvfile:

        #based on python documentation
        spamreader = csv.reader(csvfile, delimiter=']')
        for row in spamreader:
            f.write(str(row))

    f.close()

两个问题:

  1. 如何确保输出一致?换句话说,我怎样才能消除“”和换行符,以便所有 JSON 都有效?它与我添加的 nlsparams 有关吗?

  2. 最终,我不想转储到 .txt 文件,而是转储到 .json 文件。如何才能做到这一点?

感谢您的所有回复!

【问题讨论】:

  • not getting.. 表示您有 CSV 文件并创建 JSON 文件。 ?
  • 我不创建 .json 文件。 .json 文件已创建,但为空。我有一个 csv 文件,其中每一行都是一系列 JSON 对象,以 [ 开头并以 ] 结尾。这个想法是我想从这个 csv 文件中读取(逐行)并将这些值传递给 .json 文件。问题是第一行读完后,后面的其他行都读错了。
  • 您的第一个文件似乎只是 json。您可以在阅读后尝试import jsonjson.loads(your_file)(在您的代码中使用json.loads(csvfile.read()) 而不是使用csv.reader())吗?
  • 如果只有一列,为什么要将文件读取为 csv 文件?为什么不使用 json 模块?
  • 是的,使用 json 模块

标签: python json csv


【解决方案1】:

我的猜测-未经测试-:

import json
with open('test.txt','r') as input_file:
    with open('output.json','wb') as output_file:
        for row in input_file.readlines():
            output_file.write(json.loads(row))

无论如何,使用这个库会对你有很大帮助:https://docs.python.org/2/library/json.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-26
    • 2020-10-19
    • 1970-01-01
    • 2017-12-18
    • 2015-01-30
    • 2016-11-03
    • 2017-03-11
    • 2011-02-21
    相关资源
    最近更新 更多