Skip to main content
 首页 » 编程设计

python之从 CSV 创建特定格式的 JSON 文件

2025年05月04日51leader

如果我完全错误地处理 JSON 文件,首先要道歉,我一直在努力拼凑我能做的。如果你有更好的建议,请提供。这是我的问题:

我正在尝试从包含 3 列的 CSV 创建一个 JSON 文件,如下所示:

000024F14CF24E42A5F36D7CB7A07C26,Name One,action-1 
000024F14CF24E42A5F36D7CB7A07C26,Name One Variant,action-1 
000042F8F69C4A048DDD4770DB7966C8,Name Two,action-2 

我需要完成的JSON格式是:

{ 
"topics": [ 
    { 
        "id": "000024f14cf24e42a5f36d7cb7a07c26",  
        "label": [ 
            "Name One",  
            "Name One Variant" 
        ] 
        "meta": { 
            "action": "action-1" 
        } 
    } 
    { 
        "id": "000042F8F69C4A048DDD4770DB7966C8",  
        "label": [ 
            "Name Two" 
        ] 
        "meta": { 
            "action": "action-2" 
        } 
    } 
  ] 
} 

所以基本上我需要将名称组合到一个列表中,如果它们具有相同的 ID,则保留所有变体,并且我只需要保留一个操作,因为它们每个 ID 始终相同。

到目前为止,我将在下面粘贴的脚本接近了,但我被卡住了。该脚本输出如下所示的 JSON,您可以在其中看到操作被添加到标签数组中。如何将操作分开?:

{ 
    "topics": [ 
        { 
            "id": "000024f14cf24e42a5f36d7cb7a07c26",  
            "label": [ 
                "Name One",  
                "action-1",  
                "Name One Variant",  
                "action-1" 
            ] 
        } 
    ] 
} 

脚本:

import csv 
import json 
from collections import defaultdict 
 
def convert2json(): 
    # open the CSV file and loop through each row and append to the uniques list 
    uniques = [] 
    with open('uploads/test.csv','rb') as data_file: 
        reader = csv.reader(data_file) 
        for row in reader: 
            itemids = row[0] 
            values = row[1] 
            actions = row[2] 
            uniques.append((itemids, values, actions)) 
 
    # using defaultdict create a list, then loop through uniques and append 
    output = defaultdict(list) 
    for itemid, value, action in uniques: 
        output[itemid].append(value) 
        output[itemid].append(action) 
 
 
    # loop through the defaultdict list and append values to a dictionary 
    # then add values with labels to the done list 
 
    done = [] 
    for out in output.items(): 
        jsonout = {} 
        ids = out[0] 
        jsonout['id'] = ids.lower() 
        vals = out[1] 
        jsonout['label'] = vals 
        done.append(jsonout) 
 
    # create a dictionary and add the "done" list to it so it outputs 
    # an object with a JSON array named 'topics' 
    dones = {} 
    dones['topics'] = done 
 
    print json.dumps(dones, indent=4, encoding='latin1')                                
 
if __name__ == "__main__": 
    convert2json() 

请您参考如下方法:

你确实很接近。我会立即构建结构。第一次看到 itemid 时,准备好它的条目并记住它,以后只需将值添加到标签即可。

import csv 
 
summary = {} 
with open('test.csv', 'rb') as data_file: 
    reader = csv.reader(data_file) 
    for itemid, value, action in reader: 
        if itemid not in summary: 
            summary[itemid] = dict(id=itemid, label=[value], meta={'action': action}) 
        else: 
            summary[itemid]['label'].append(value) 
 
data = {"topics": list(summary.values())}