[英]Convert csv file to json dictionary
我正在嘗試使用 python 將 csv 文件轉換為 json 字典,但我對操作 Z466DEEC76ECDF5FCA6D3824 文件的經驗為零。
csv 樣品:
Picture Class Region_count Coordinates
foto_1jpg.jpg tennis 1 "all_points_x":[154,157,230,275,278,218,160,11...
foto_1jpg.jpg soccer 2 "all_points_x":[446,557,685,795,826,815,738,62...
foto_1jpg.jpg basket 3 "all_points_x":[941,1065,1161,1310,1438,1497,1...
foto_2jpg.jpg soccer 1 "all_points_x":[331,403,518,626,688,734,758,681,...
foto_2jpg.jpg basket 2 "all_points_x":[972,887,830,802,789,804,857,96...
我已經使用以下代碼轉換了 csv 文件:
import csv
import json
def csv_to_json(csvFilePath, jsonFilePath):
jsonArray = []
#read csv file
with open(csvFilePath, encoding='utf-8') as csvf:
#load csv file data using csv library's dictionary reader
csvReader = csv.DictReader(csvf)
#convert each csv row into python dict
for row in csvReader:
#add this python dict to json array
jsonArray.append(row)
#convert python jsonArray to JSON String and write to file
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonString = json.dumps(jsonArray, indent=4)
jsonf.write(jsonString)
csvFilePath = r'/content/test_set.csv'
jsonFilePath = r'train.json'
csv_to_json(csvFilePath, jsonFilePath)
此轉換的結果是具有以下結構的 json 列表:
[
{
"Picture": "foto_1jpg.jpg",
"Class": "tennis",
"Region_count": "1",
"Coordinates": "\"all_points_x\":[154,157,230,275,278,218,160,112,113,154],\"all_points_y\":[461,461,455,495,576,625,625,563,505,463]"
},
{
"Picture": "foto_1jpg.jpg",
"Class": "soccer",
"Region_count": "2",
"Coordinates": "\"all_points_x\":[446,557,685,795,826,815,738,628,505,422,346,331,354,443],\"all_points_y\":[230,186,212,321,411,538,641,687,684,632,525,426,331,224]"
},
{
"Picture": "foto_2jpg.jpg",
"Class": "soccer",
"Region_count": "1",
"Coordinates": "all_points_x:[331,403,518,626,688,734,758,681,594,484,369,314,282,274,329],\"all_points_y\":[399,340,316,342,380,463,607,736,787,796,745,683,592,503,405]"
}
]
我的目標是獲得具有以下結構的 json 字典:
{"foto_1jpg.jpg121349":
{"filename":"foto_1jpg.jpg","regions":[
{"shape_attributes":{"name":"polygon","all_points_x":[154,157,230,275,278,218,160,112,113,154],"all_points_y":[461,461,455,495,576,625,625,563,505,463]},"region_attributes":{"name":"tennis"}},
{"shape_attributes":{"name":"polygon","all_points_x":[446,557,685,795,826,815,738,628,505,422,346,331,354,443],"all_points_y":[230,186,212,321,411,538,641,687,684,632,525,426,331,224]},"region_attributes":{"name":"soccer"}},
{"shape_attributes":{"name":"polygon","all_points_x":[941,1065,1161,1310,1438,1497,1509,1471,1382,1279,1124,998,916,874,847,874,938],"all_points_y":[132,44,26,48,144,266,396,514,628,673,687,631,560,479,328,233,135]},"region_attributes":{"name":"basket"}}],"file_attributes":{}},
"foto_2.jpg325912":
{"filename":"foto_2.jpg","regions":[
{"shape_attributes":{"name":"polygon","all_points_x":[331,403,518,626,688,734,758,681,594,484,369,314,282,274,329],"all_points_y":[399,340,316,342,380,463,607,736,787,796,745,683,592,503,405]},"region_attributes":{"name":"soccer"}},
{"shape_attributes":{"name":"polygon","all_points_x":[1186,1233,1273,1282,1267,1231,1178,1154,1135,1131,1142,1182],"all_points_y":[921,921,891,845,806,777,775,789,819,859,895,919]},"region_attributes":{"name":"tennis"}}],"file_attributes":{}}
我嘗試使用以下代碼將密鑰“區域”插入 json 文件:
import csv
from collections import defaultdict
def ctree():
return defaultdict(ctree)
def build_leaf(name, leaf):
res = {"Picture": name}
if len(leaf.keys()) :
res["Region"] = [build_leaf(k, v) for k, v in leaf.items()]
return res
def main():
tree = ctree()
with open('/content/templete.csv') as csvfile:
reader = csv.reader(csvfile)
for rid, row in enumerate(reader):
if rid == 0:
continue
leaf = tree[row[0]]
for cid in range(1, len(row)):
leaf = leaf[row[cid]]
res = []
for name, leaf in tree.items():
res.append(build_leaf(name, leaf))
import json
return json.dumps(res)
main()
結果是一個巨大的失敗:
[
{"Picture": "foto_1jpg.jpg", "Region": [
{"Picture": "tennis", "Region": [{"Picture": "1", "Region": [{"Picture": "\"all_points_x\":[154,157,230,275,278,218,160,112,113,154],\"all_points_y\":[461,461,455,495,576,625,625,563,505,463]"}]}]},
{"Picture": "soccer", "Region": [{"Picture": "2", "Region": [{"Picture": "\"all_points_x\":[446,557,685,795,826,815,738,628,505,422,346,331,354,443],\"all_points_y\":[230,186,212,321,411,538,641,687,684,632,525,426,331,224]"}]}]},
{"Picture": "basket", "Region": [{"Picture": "3", "Region": [{"Picture": "\"all_points_x\":[941,1065,1161,1310,1438,1497,1509,1471,1382,1279,1124,998,916,874,847,874,938],\"all_points_y\":[132,44,26,48,144,266,396,514,628,673,687,631,560,479,328,233,135]"}]}]}]}
我怎樣才能達到我的目標結構?
感謝關注
嘗試在 json VIA 中進行轉換:
import csv, json
li = []
with open('/content/file.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
li.append({
"filename": row['Picture'],
"region": {
"shape_attributes":{
"name":"polygon",
"Coordinates": row['Coordinates'],
"region_attribute":{
"name": row['Class']}}}})
with open("file.json", "w") as f:
json.dump(li, f, indent=4)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.