繁体   English   中英

将 csv 文件转换为 json 字典

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM