如何从 CSV 文件转换为键控格式的 JSON?
How to convert to JSON in a Keyed Format from a CSV file?
目前,我正在使用以下代码创建 JSON
:
import pandas as pd
my_csv = pd.DataFrame(pd.read_csv("output.csv", sep = ",", header = 0, index_col = False, encoding='utf-8-sig'))
json_file = my_csv.to_json( orient = "records", date_format = "epoch", double_precision = 10, date_unit = "ms", default_handler = None)
file = open("newJsonfile.json", "w")
file.write(json_file)
file.close()
输出是这样的:
{
"title": "Sample Title 1 here",
"description": "Sample description 1 here"
},
{
"title": "Sample Title 2 here",
"description": "Sample description 2 here"
},
但是,这个网站:https://www.convertcsv.com/csv-to-json.htm 有一个 Keyed JSON
选项,在解析后收到这个输出:
{
"1": {
"title": "Sample Title 1 here",
"description": "
},
"2": {
"title": "Sample Title 2 here",
"description": "Sample description 2 here"
}
如何使用 Python
实现此目的,因为每次打开此网站并进行转换是不可行的?
keyed = {}
for idx,x in enumerate(a):
keyed[str(idx)]=x
我在你的问题中加载数据:
data = [{
"title": "Sample Title 1 here",
"description": "Sample description 1 here"
},
{
"title": "Sample Title 2 here",
"description": "Sample description 2 here"
}]
df = pd.DataFrame(data)
print(df)
title description
0 Sample Title 1 here Sample description 1 here
1 Sample Title 2 here Sample description 2 here
那么,df.to_json()
和orient='index'
就可以了
json_file = df.to_json(orient='index',indent=4)
print(json_file)
{
"0":{
"title":"Sample Title 1 here",
"description":"Sample description 1 here"
},
"1":{
"title":"Sample Title 2 here",
"description":"Sample description 2 here"
}
}
如果你想从 1 而不是 0 开始索引,
你可以在 df.to_json()
之前重建索引
df.index = range(1,df.shape[0]+1)
json_file = df.to_json(orient='index',indent=4)
print(json_file)
{
"1":{
"title":"Sample Title 1 here",
"description":"Sample description 1 here"
},
"2":{
"title":"Sample Title 2 here",
"description":"Sample description 2 here"
}
}
目前,我正在使用以下代码创建 JSON
:
import pandas as pd
my_csv = pd.DataFrame(pd.read_csv("output.csv", sep = ",", header = 0, index_col = False, encoding='utf-8-sig'))
json_file = my_csv.to_json( orient = "records", date_format = "epoch", double_precision = 10, date_unit = "ms", default_handler = None)
file = open("newJsonfile.json", "w")
file.write(json_file)
file.close()
输出是这样的:
{
"title": "Sample Title 1 here",
"description": "Sample description 1 here"
},
{
"title": "Sample Title 2 here",
"description": "Sample description 2 here"
},
但是,这个网站:https://www.convertcsv.com/csv-to-json.htm 有一个 Keyed JSON
选项,在解析后收到这个输出:
{
"1": {
"title": "Sample Title 1 here",
"description": "
},
"2": {
"title": "Sample Title 2 here",
"description": "Sample description 2 here"
}
如何使用 Python
实现此目的,因为每次打开此网站并进行转换是不可行的?
keyed = {}
for idx,x in enumerate(a):
keyed[str(idx)]=x
我在你的问题中加载数据:
data = [{
"title": "Sample Title 1 here",
"description": "Sample description 1 here"
},
{
"title": "Sample Title 2 here",
"description": "Sample description 2 here"
}]
df = pd.DataFrame(data)
print(df)
title description
0 Sample Title 1 here Sample description 1 here
1 Sample Title 2 here Sample description 2 here
那么,df.to_json()
和orient='index'
就可以了
json_file = df.to_json(orient='index',indent=4)
print(json_file)
{
"0":{
"title":"Sample Title 1 here",
"description":"Sample description 1 here"
},
"1":{
"title":"Sample Title 2 here",
"description":"Sample description 2 here"
}
}
如果你想从 1 而不是 0 开始索引,
你可以在 df.to_json()
df.index = range(1,df.shape[0]+1)
json_file = df.to_json(orient='index',indent=4)
print(json_file)
{
"1":{
"title":"Sample Title 1 here",
"description":"Sample description 1 here"
},
"2":{
"title":"Sample Title 2 here",
"description":"Sample description 2 here"
}
}