[英]CSV to JSON convert using python
Good afternoon, I don't have a background on python, and i tried some pre made code that is published on the internet and stack overflow but i don't get the result i want.下午好,我没有 python 背景,我尝试了一些在互联网上发布的预制代码和堆栈溢出,但我没有得到我想要的结果。 here is my reference: https://www.geeksforgeeks.org/convert-csv-to-json-using-python .
这是我的参考: https : //www.geeksforgeeks.org/convert-csv-to-json-using-python 。 maybe someone can help me with a simple code, i want to convert this csv format
也许有人可以用简单的代码帮助我,我想转换这种 csv 格式
appname![]() |
hostname![]() |
id ![]() |
---|---|---|
backend![]() |
testserver1![]() |
1 ![]() |
frontend![]() |
testserver2![]() |
2 ![]() |
database![]() |
testserver3![]() |
3 ![]() |
into a json format that looks like this变成像这样的 json 格式
{
"appname": ["backend","frontend","database"],
"hostname": ["testserver1","testserver2","testserver3"],
"id": ["1","2","3"]
}
What im currently using:我目前使用的是什么:
import csv
import json
# Function to convert a CSV to JSON
# Takes the file paths as arguments
def make_json(csvFilePath, jsonFilePath):
# create a dictionary
data = {}
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
# Convert each row into a dictionary
# and add it to data
for rows in csvReader:
# Assuming a column named 'No' to
# be the primary key
key = rows['appname']
data[key] = rows
# Open a json writer, and use the json.dumps()
# function to dump data
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
# Driver Code
# Decide the two file paths according to your
# computer system
csvFilePath = r'Names.csv'
jsonFilePath = r'Names.json'
# Call the make_json function
make_json(csvFilePath, jsonFilePath)
output from the code:代码输出:
{
"backend": {
"appname": "backend",
"hostname": "testserver1",
"ami_id": "1"
},
"frontend": {
"appname": "frontend",
"hostname": "testserver2",
"ami_id": "2"
},
"database": {
"appname": "database",
"hostname": "testserver3",
"ami_id": "3"
}
The result is want:结果是想要:
{
"appname": ["backend","frontend","database"],
"hostname": ["testserver1","testserver2","testserver3"],
"id": ["1","2","3"]
}
If you print each dictionary during row in csvReader
loop you'll see:如果您
row in csvReader
循环中的row in csvReader
期间打印每个字典,您将看到:
{'appname': 'backend', 'hostname': 'testserver1', 'id': '1'}
{'appname': 'frontend', 'hostname': 'testserver2', 'id': '2'}
{'appname': 'database', 'hostname': 'testserver3', 'id': '3'}
So you need to modify the loop to get desired behavior:因此,您需要修改循环以获得所需的行为:
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
# Convert each row into a dictionary
# and add it to data
for row in csvReader:
for columnName in row:
if columnName not in data:
data[columnName] = []
data[columnName].append(row[columnName])
The output JSON file will look like:输出 JSON 文件将如下所示:
{
"appname": [
"backend",
"frontend",
"database"
],
"hostname": [
"testserver1",
"testserver2",
"testserver2"
],
"id": [
"1",
"2",
"3"
]
}
Unless your input file is really huge I would suggest using pandas:除非您的输入文件非常大,否则我建议使用 Pandas:
import pandas as pd
csv_df = pd.read_csv('stack_69581387.csv')
d= {'appname': csv_df.appname.tolist(),
'hostname': csv_df.hostname.tolist(),
'id': csv_df.id.tolist()}
If you want to have only unique values (it is not specified in your question what logic should be applied if you have redundancies in the data) you may use:如果您只想拥有唯一值(在您的问题中未指定如果数据中有冗余应应用什么逻辑),您可以使用:
import pandas as pd
csv_df = pd.read_csv('stack_69581387.csv')
d = {'appname': csv_df.appname.unique().tolist(),
'hostname': csv_df.hostname.unique().tolist(),
'id': csv_df.id.unique().tolist()}
And, to write it:而且,写它:
import json
with open('data_69581387.json', 'w') as outfile:
json.dump(d, outfile)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.