将复杂的JSON文件解析为CSV [Magic the Gathering]

时间:2017-09-07 02:35:50

标签: json python-3.x csv parsing export-to-csv

我正在尝试将包含所有Magic the Gathering卡信息的JSON文件解析为平面csv文件。只是尝试在操作具有不同数据类型的数据集时练习 - 可以在此处找到源json文件(单击“All Sets”以下载JSON文件):https://mtgjson.com/

可以在上述网站的文档选项卡中找到其他文档(即上述JSON文件中的所有字段)。

JSON文件的结构方式是> 200套,每套都有很多卡。我有下面的代码,它成功地将给定集合中每张卡片的所有必要卡级别信息(如法术力费用,卡片名称,卡片颜色等)写入CSV文件。但是,我还要向CSV写出信息,这是一个高于卡级别信息的级别 - 即设置级别信息。这些是“名称”,“代码”,......“助推器”之类的内容,您可以在以下屏幕截图中看到:https://imgur.com/a/olNvr。理想情况下,任何设置级别信息都将是CSV文件中的前几列,然后是卡级别信息。基本上是“平面”类型的CSV文件。

import json
import csv

sets = ['UNH','UGL','pWOS','pWOR','pWCQ','pSUS','pSUM','pREL','pPRO','pPRE','pPOD','pMPR','pMGD','pMEI','pLPA','pLGM','pJGP','pHHO','pWPN','pGTW','pGRU','pGPX','pFNM','pELP','pDRC','pCMP','pCEL','pARL','pALP','p2HG','p15A','PD3','PD2','H09','PTK','POR','PO2','PCA','PC2','HOP','VMA','MMA','MM3','MM2','MED','ME4','ME3','ME2','EMA','MPS_AKH','MPS','EXP','V16','V15','V14','V13','V12','V11','V10','V09','DRB','EVG','DDS','DDR','DDQ','DDP','DDO','DDN','DDM','DDL','DDK','DDJ','DDI','DDH','DDG','DDF','DDE','DDD','DDC','DD3_JVC','DD3_GVL','DD3_EVG','DD3_DVD','DD2','CNS','CN2','CMD','CMA','CM1','C17','C16','C15','C14','C13','CEI','CED','E01','ARC','ZEN','WWK','WTH','W17','W16','VIS','VAN','USG','ULG','UDS','TSP','TSB','TPR','TOR','TMP','THS','STH','SOM','SOK','SOI','SHM','SCG','S99','S00','RTR','RQS','ROE','RAV','PLS','PLC','PCY','ORI','ONS','OGW','ODY','NPH','NMS','MRD','MOR','MMQ','MIR','MGB','MD1','MBS','M15','M14','M13','M12','M11','M10','LRW','LGN','LEG','LEB','LEA','KTK','KLD','JUD','JOU','ITP','ISD','INV','ICE','HOU','HML','GTC','GPT','FUT','FRF_UGIN','FRF','FEM','EXO','EVE','EMN','DTK','DST','DRK','DPA','DKM','DKA','DIS','DGM','CST','CSP','CP3','CP2','CP1','CON','CHR','CHK','BTD','BRB','BOK','BNG','BFZ','AVR','ATQ','ATH','ARN','ARB','APC','ALL','ALA','AKH','AER','9ED','8ED','7ED','6ED','5ED','5DN','4ED','3ED','2ED','10E']

with open('AllSets.json', encoding = 'utf8') as f:
    json_data = json.load(f)

    header = ['id', 'layout', 'name', 'names', 'manaCost', 'cmc', 'colors', 'colorIdentity', 'type', 'superTypes', 'types', 'subtypes', 'rarity', 'text', 'flavor', 'artist', 'number', 'power', 'toughness', 'loyalty', 'multiverseid', 'variations', 'imageName', 'watermark', 'border', 'timeshifted', 'hand', 'life', 'reserved', 'releaseDate', 'starter', 'mciNumber']

    # create the csv writer object
    csvwriter = csv.DictWriter(open('testData.csv', 'w', newline='', encoding='utf-8'), fieldnames = header, extrasaction='ignore')

    csvwriter.writeheader()

    for set in sets:

        set_data = json_data[set]['cards']

        for card in set_data:

            csvwriter.writerow(card)

我认为在for card in set_data循环中我们需要能够逐行将设置级别信息写入CSV文件的代码。我为集合的特定代码字段尝试了csvwriter.writerow(set['code'])之类的内容,但不幸的是,这会产生TypeError: string indices must be integers错误。非常感谢任何建议!

0 个答案:

没有答案