熊猫读取xml时出现奇怪的标签不匹配错误

问题描述

我一直在使用包 pandas_read_xml 将 XML 文件读入 Pandas 数据帧。但是,我最近开始体验这个包的非常奇怪的行为。 xml 解析器偶尔会崩溃,但经过反复尝试,它可以工作。我真的对此感到困惑,所以我希望这里的任何人都可以帮助我解决这个问题。我将尝试在下面说明我面临的问题。

  import pandas as pd
  import pandas_read_xml as pdx

  data = pdx.read_xml('https://www.sec.gov/Archives/edgar/data/1000351/000114554921012283/primary_doc.xml',['edgarSubmission'])

这偶尔会返回错误“ExpatError: mismatched tag: line 50,column 124”。然而,它在反复尝试后工作得很好。对于其他路径也观察到类似的行为。我已经确保 xml 文件没有任何问题。我查看了 Traceback,它包含以下内容

 File "<ipython-input-118-c68fdb3a2633>",line 1,in <module>
 data = pdx.read_xml('https://www.sec.gov/Archives/edgar/data/1002537/000114554921006264/primary_doc.xml',['edgarSubmission'])

 File "C:\Users\A1610222\AppData\Local\Continuum\anaconda2\lib\site-packages\pandas_read_xml.py",line 33,in read_xml return read_xml_as_dataframe(read_xml_from_url(path_or_xml),root_key_list,root_is_rows=root_is_rows,transpose=transpose)

 File "C:\Users\A1610222\AppData\Local\Continuum\anaconda2\lib\site-packages\pandas_read_xml.py",line 62,in read_xml_as_dataframe return pd.DataFrame([get_to_root_in_dict(xmltodict.parse(xml),root_key_list)])

 File "C:\Users\A1610222\AppData\Local\Continuum\anaconda2\lib\site-packages\xmltodict.py",line 327,in parse parser.Parse(xml_input,True)

 ExpatError: mismatched tag: line 50,column 124

它似乎指向包 pandas_read_xml 中的第 33 行和第 62 行。我已经卸载并重新安装了该软件包以确保没有任何问题,但问题仍然存在。如果我遗漏了一些完全基本的东西,请原谅我的无知。如果有任何不清楚的地方,请告诉我。期待您的帮助。

解决方法

我今天发现问题是由于连接问题引起的,与包或 xml 文件的结构无关。