Python和CSV:从CSV

时间:2015-05-15 13:26:20

标签: python json csv

我有一个从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文件。如何才能做到这一点?

  3. 感谢您的回复!

1 个答案:

答案 0 :(得分:0)

我的猜测 - 不测试 - :

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

在任何情况下,使用此lib都会对您有所帮助:https://docs.python.org/2/library/json.html