Python xmltodict.parse 返回异常"格式不正确(无效标记):第 6 行,第 15 列



使用下面的代码片段,我不确定如何解决坏元素/属性。它似乎被正确引用,并在适当的utf-8形式(我相信)。但是x07出错了xmltodict.parse

异常:格式不正确(无效标记):第6行,第15列

任何想法如何剥离这些代码点,使它不会抛出异常?

response = requests.get(dp_url, params=dp_params)
try:
dict_response = xmltodict.parse(response.text)
except Exception as e:   ***not well-formed (invalid token): line 6, column 15***
print(e)

XML:

<result><record><field name='donor_id' id='donor_id' value='40362'/><field name='first_name' id='first_name' value='John'/><field name='org_rec' id='org_rec' value='N'/><field name='donor_type' id='donor_type' value='IN'/><field name='nomail' id='nomail' value='N'/><field name='nomail_reason' id='nomail_reason' value=''/><field name='narrative' id='narrative' value='2/26/2021 - TD: added Louise to record. Check only has her name and didn&apos;t return the reply device.rn3/17/2015 - MS: Removed an extra sopace between Spring and St in Address field. rnrn8/26/2014 - MS: Moved initial to Middle Name field.rnrn11/14/2012 TD: x07 telephone number added per telephone campaign 2012'/><field name='tag_date' id='tag_date' value=''/><field name='quickbooks_customer_id' id='quickbooks_customer_id' value=''/></record></result>

您是否尝试过替换有问题的字符?

xmltodict.parse(response.text.replace('x07', ''))

最新更新