0
for i in range(1,3200):
    input_file = 'ProductGet'
    fp = open(input_file+str(i)+'.xml' ,encoding='utf8')
    tree = etree.parse(fp)
    ProductID = tree.xpath('/Responses/Response/Data/Product/ProductID/text()')
    with open(input_file+str(i)+'.xml', errors='ignore') as fd:
        doc = xmltodict.parse(fd.read())
    berrak = doc['Responses']['Response']['Data']['Product']
    for ii in range(0,len(ProductID)):
        print(berrak[ii]['Variations']['Variation']['VariationIsLive'])

我有一些 .xml 文件集,我的脚本从 1 到 3200 打开所有这些文件。但是当它随机解析它时,它会显示这部分NoneType的消息。berrak[ii]['Variations']['Variation']['VariationIsLive']我检查了这个文件,一切看起来都很好。我的问题是如何将这个 NoneType 元素转换为字符串?

4

1 回答 1

0

您应该保护您的代码免受此问题的影响

for ii in range(0,len(ProductID)):
    variations = berrak[ii].get('Variations')
    if variations:
        variation = variations.get(''Variation'')
        if variation:
            is_live = variation.get('VariationIsLive')
            if is_live is not None:
                print(is_live) 
于 2019-03-14T10:13:35.603 回答