来自CSV的嵌套字典



我有一个这样格式化的CSV文件,注意同一图像有多个名称:

yMax858a0246-2f2d-40a9-9bcb-01ab8a93c7f5_BU26844_1630586024_0_6。413858a0246-2f2d-40a9-9bcb-01ab8a93c7f5_BU26844_1630586024_0_6。857858a0246-2f2d-40a9-9bcb-01ab8a93c7f5_BU26844_1630586024_23。343

考虑到您的情况,我认为这是您所期望的,下面我提供的代码将给出您正在寻找的确切输出。

import csv
file = open('out1.csv')
csvreader = csv.reader(file)
header = []
header = next(csvreader)
dic = {}
for row in csvreader:
if row[0] not in dic:
dic[row[0]] = []
dic[row[0]].append({header[i] :row[i]  for i in range(1, len(row))})
print(dic)

希望这个解决方案有帮助。如果没有,请随意评论。由于

PS:您可以添加int()作为数字。

编辑:对图像id进行uniq检查

修改代码(假设输入文件是制表符分隔的Excel)

import csv
import itertools
import operator
import json
def removekey(d, key):
' Returns dictionary with entry key removed '
r = dict(d)
del r[key]
return r
with open('out1.csv', 'r') as fp:
reader = csv.DictReader(fp, dialect='excel', skipinitialspace=True)
#headers = next(reader)         # Edit -- handled by reader for dict
new_dict = {}
for group, records in itertools.groupby(reader, key=operator.itemgetter('image')):
# Remove image key from each dictionary in records
new_dict[group] = [removekey(d, 'image') for d in records]
json_object = json.dumps(new_dict, indent = 4)
print(json_object)

{
"858a0246-2f2d-40a9-9bcb-01ab8a93c7f5_BU26844_1630586024_0_6.webp": [
{
"id": "1",
"name": "Scratch",
"xMin": "604",
"xMax": "893",
"yMin": "230",
"yMax": "413"
},
{
"id": "2",
"name": "Dent",
"xMin": "921",
"xMax": "1146",
"yMin": "720",
"yMax": "857"
}
],
"858a0246-2f2d-40a9-9bcb-01ab8a93c7f5_BU26844_1630586024_23.webp": [
{
"id": "1",
"name": "Dent",
"xMin": "343",
"xMax": "2323",
"yMin": "334",
"yMax": "343"
}
]
}

注:输入文件:(逗号分隔值)

image,id,name,xMin,xMax,yMin,yMax
858a0246-2f2d-40a9-9bcb-01ab8a93c7f5_BU26844_1630586024_0_6.webp,1,Scratch,604,893,230,413
858a0246-2f2d-40a9-9bcb-01ab8a93c7f5_BU26844_1630586024_0_6.webp,2,Dent,921,1146,720,857
858a0246-2f2d-40a9-9bcb-01ab8a93c7f5_BU26844_1630586024_23.webp,1,Dent,343,2323,334,343

相关内容

  • 没有找到相关文章

最新更新