Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Csv到Json:-使用Csv.extend函数,但在连接时获得额外的行字符[_Python_Json_Python 2.7_Csv - Fatal编程技术网

Python Csv到Json:-使用Csv.extend函数,但在连接时获得额外的行字符[

Python Csv到Json:-使用Csv.extend函数,但在连接时获得额外的行字符[,python,json,python-2.7,csv,Python,Json,Python 2.7,Csv,我有一个csv文件与以下内容 signOffDate,fundEntityType,fundId,benchmarkIds,source,returnType 30-Sep-17,ENTITY_ID,44182,MAN,IP,TR 30-Nov-17,ENTITY_ID,44182,VAN,IP,TR 我想使用python将转换为包含内容的json文件 { "fundId": [ "44182"], "signOffDate": "30-Sep-17", "f

我有一个csv文件与以下内容

signOffDate,fundEntityType,fundId,benchmarkIds,source,returnType
30-Sep-17,ENTITY_ID,44182,MAN,IP,TR    
30-Nov-17,ENTITY_ID,44182,VAN,IP,TR
我想使用python将转换为包含内容的json文件

 {
    "fundId": [ "44182"],
    "signOffDate": "30-Sep-17",
    "fundEntityType": "ENTITY_ID",
    "source": "IP",
    "returnType": "TR",
    "benchmarkIds": [ "MAN"]
},
{
    "fundId": ["44182"],
    "signOffDate": "30-Nov-17",
    "fundEntityType": "ENTITY_ID",
    "source": "IP",
    "returnType": "TR",
    "benchmarkIds": [ "VAN"]
}
到目前为止我已经尝试过的python代码如下。它有一些问题。我被困在这里

import csv
import json

csvfile ='sample.csv'
jsonfile ='sample.json'


def read_CSV(file, json_file):
    csv_rows = []
    with open(file) as csvfile:
        reader = csv.DictReader(csvfile)
        field = reader.fieldnames
        for row in reader:
                   csv_rows.extend([{field[i]:[row[field[i]]] if field[i]=='benchmarkIds' or  field[i]=='fundId' else row[field[i]] for i in range(len(field))}]);
    convert_write_json(csv_rows, json_file)

#Convert csv data into json
def convert_write_json(data, json_file):
    with open(json_file, "w") as f:
        f.write(json.dumps(data, sort_keys=False, indent=4, separators=(',', ': ')))
        #f.write(json.dumps(data))


read_CSV(csvfile,jsonfile)

您需要使用
append
而不是
extend

Ex:

import csv
import json
import pprint

with open("file", "r") as csvfile:
    reader = csv.DictReader(csvfile)
    field = reader.fieldnames
    csv_rows = []
    for row in reader:
        csv_rows.append({field[i]:[row[field[i]]] if field[i]=='benchmarkIds' or  field[i]=='fundId' else row[field[i]] for i in range(len(field))})

for i in csv_rows:
    pprint.pprint(i)

print(json.dumps(csv_rows))
{'benchmarkIds': ['MAN'],
 'fundEntityType': 'ENTITY_ID',
 'fundId': ['44182'],
 'returnType': 'TR    ',
 'signOffDate': '30-Sep-17',
 'source': 'IP'}
{'benchmarkIds': ['VAN'],
 'fundEntityType': 'ENTITY_ID',
 'fundId': ['44182'],
 'returnType': 'TR',
 'signOffDate': '30-Nov-17',
 'source': 'IP'}

[{"fundId": ["44182"], "signOffDate": "30-Sep-17", "fundEntityType": "ENTITY_ID", "source": "IP", "returnType": "TR    ", "benchmarkIds": ["MAN"]}, {"fundId": ["44182"], "signOffDate": "30-Nov-17", "fundEntityType": "ENTITY_ID", "source": "IP", "returnType": "TR", "benchmarkIds": ["VAN"]}]
输出:

import csv
import json
import pprint

with open("file", "r") as csvfile:
    reader = csv.DictReader(csvfile)
    field = reader.fieldnames
    csv_rows = []
    for row in reader:
        csv_rows.append({field[i]:[row[field[i]]] if field[i]=='benchmarkIds' or  field[i]=='fundId' else row[field[i]] for i in range(len(field))})

for i in csv_rows:
    pprint.pprint(i)

print(json.dumps(csv_rows))
{'benchmarkIds': ['MAN'],
 'fundEntityType': 'ENTITY_ID',
 'fundId': ['44182'],
 'returnType': 'TR    ',
 'signOffDate': '30-Sep-17',
 'source': 'IP'}
{'benchmarkIds': ['VAN'],
 'fundEntityType': 'ENTITY_ID',
 'fundId': ['44182'],
 'returnType': 'TR',
 'signOffDate': '30-Nov-17',
 'source': 'IP'}

[{"fundId": ["44182"], "signOffDate": "30-Sep-17", "fundEntityType": "ENTITY_ID", "source": "IP", "returnType": "TR    ", "benchmarkIds": ["MAN"]}, {"fundId": ["44182"], "signOffDate": "30-Nov-17", "fundEntityType": "ENTITY_ID", "source": "IP", "returnType": "TR", "benchmarkIds": ["VAN"]}]

你似乎表示你的代码有问题,但我在任何地方都看不到代码…你的问题到底是什么?你的代码在哪里?它在我可以添加之前就提交了。请运行我的代码。你会看到问题。在这个文件中=sample.csv??是的。这是你的源cvs文件在你的代码中发现了一个错误。我想要json文件中的输出ch我们对属性和值使用双引号。您的o/p使用单引号。这是因为我没有将其转换为JSON,因为我已经知道如何转换,并且使用pprint显示输出。无论如何,我已经更新了代码段。在python 3中,使用print(JSON.dumps(csv_rows))