XML 去字典提取

XML to dictionary extraction

我写了一段代码,但 ecd 中的一些值丢失了。我想将它们指示为 'None'0000 以便能够创建数据框。不幸的是,代码运行到缺少的地方然后崩溃,我无法发现错误。

错误信息:

File "extra.py", line 236, in <module>
    if dic['mudLogs']['mudLog']['geologyInterval'][i]['ecdTdAv']['#text'] != None:
  KeyError: 'ecdTdAv'

代码:

xml_file = 'C:\Users\jtfra\Desktop\Thesis\Volve_Real_Time_DData\WITSML Realtime drilling data\Norway-Statoil-NO 15_$_9-F-11\1\mudLog\1.xml'

def convert(xml_file, xml_attribs=True):
with open(xml_file, "rb") as f:    # notice the "rb" mode
    d = xmltodict.parse(f, xml_attribs=xml_attribs)
    return d

dic = convert(xml_file)
mdTop, ecd = [], []


 for i in range(len(dic['mudLogs']['mudLog']['geologyInterval'])):
    mdTop.append(dic['mudLogs']['mudLog']['geologyInterval'][i]['mdTop']['#text'])

    if dic['mudLogs']['mudLog']['geologyInterval'][i]['ecdTdAv']['#text'] != None:
        ecd.append(dic['mudLogs']['mudLog']['geologyInterval'][i]['ecdTdAv']['#text'])
    else:
        ecd.append('None')

print(ecd)

而不是访问它:

dic['mudLogs']['mudLog']['geologyInterval'][0]['ecdTdAv']

做:

dic['mudLogs']['mudLog']['geologyInterval'][0].get('ecdTdAv', '0000')

或类似。

您还可以检查密钥是否存在:

if 'ecdTdAv' in dic['mudLogs']['mudLog']['geologyInterval'][I]:
    # do something with it, e.g.:
    print(dic['mudLogs']['mudLog']['geologyInterval'][i]['ecdTdAv']['#text'])