【问题标题】:How to solve Key Error while XML File Parsing in PythonPython解析XML文件时如何解决Key Error
【发布时间】:2020-10-20 22:09:45
【问题描述】:

我有以下 XML 文件,我想将其转换为 Pandas DataFrame。

row {'Id': '-1', 'Reputation': '1', 'CreationDate': '2009-09-28T00:00:00.000', 'DisplayName': 'Community', 'LastAccessDate': '2010-11-10T17:25:34.627', 'WebsiteUrl': 'http://meta.stackexchange.com/', 'Location': 'on the server farm', 'AboutMe': '<p>Hi, I\'m not really a person.</p>\n\n<p>I\'m a background process that helps keep this site clean!</p>\n\n<p>I do things like</p>\n\n<ul>\n<li>Randomly poke old unanswered questions every hour so they get some attention</li>\n<li>Own community questions and answers so nobody gets unnecessary reputation from them</li>\n<li>Own downvotes on spam/evil posts that get permanently deleted</li>\n<li>Own suggested edits from anonymous users</li>\n<li><a href="http://meta.stackexchange.com/a/92006">Remove abandoned questions</a></li>\n</ul>\n', 'Views': '0', 'UpVotes': '21001', 'DownVotes': '27468', 'AccountId': '-1'}
row {'Id': '1', 'Reputation': '21228', 'CreationDate': '2009-09-28T14:35:46.490', 'DisplayName': 'Anton Geraschenko', 'LastAccessDate': '2020-05-17T06:51:32.333', 'WebsiteUrl': 'http://stacky.net', 'Location': 'Palo Alto, CA, United States', 'AboutMe': '<p>You can get in touch with me at geraschenko@gmail.com.</p>\n', 'Views': '25360', 'UpVotes': '1052', 'DownVotes': '90', 'AccountId': '36500'}

以下代码适用于几乎相同的 XML 文件,但是当我将它用于此文件时,我收到错误:

代码

users_tree = ET.parse("/content/Users.xml")
users_root = users_tree.getroot()

file_path_users = r"/content/Users.xml"
dict_list_users = []

for _, elem in ET.iterparse(file_path_users, events=("end",)):
    if elem.tag == "row":
        dict_list_users.append({'UserId': elem.attrib['Id'],
                          'Reputation': elem.attrib['Reputation'],
                          'CreationDate': elem.attrib['CreationDate'],
                          'DisplayName': elem.attrib['DisplayName'],
                          'LastAccessDate': elem.attrib['LastAccessDate'],
                          'WebsiteUrl': elem.attrib['WebsiteUrl'],
                          'Location': elem.attrib['Location'],
                          'AboutMe': elem.attrib['AboutMe'],
                          'Views': elem.attrib['Views'],
                          'UpVotes': elem.attrib['UpVotes'],
                          'DownVotes': elem.attrib['DownVotes'],
                          'AccountId': elem.attrib['AccountId']})
elem.clear()

df_users = pd.DataFrame(dict_list_users)

错误

KeyError                                  Traceback (most recent call last)
<ipython-input-18-7af87798bae8> in <module>()
     24                           'DisplayName': elem.attrib['DisplayName'],
     25                           'LastAccessDate': elem.attrib['LastAccessDate'],
---> 26                           'WebsiteUrl': elem.attrib['WebsiteUrl'],
     27                           'Location': elem.attrib['Location'],
     28                           'AboutMe': elem.attrib['AboutMe'],

KeyError: 'WebsiteUrl'

注意:LastAccessDate 之后的所有属性都会出现此错误,即,即使我删除了 WebsiteUrl 键,下一个属性也会出错,依此类推。

请提供解决此问题的方法。

【问题讨论】:

    标签: python-3.x xml pandas dataframe xml-parsing


    【解决方案1】:

    错误似乎是由于一个或多个&lt;row&gt; 标记中缺少属性。考虑检索 all 属性,而不是通过每个属性显式分配字典键/值。这样做,最终的DataFrame 构造函数会将NAs 输入到缺少属性的行中。

    for _, elem in ET.iterparse(file_path_users, events=("end",)):
        if elem.tag == "row":
            dict_list_users.append(elem.attrib)    # RETRIEVE ALL ATTRIBUTES
    
            elem.clear()                           # SHOULD BE AT NESTED LEVEL
    
    df_users = pd.DataFrame(dict_list_users)
    

    如果上面拉入的列比需要的多,请仅保留带有reindex 的相关列:

    df_users = df_users.reindex(
        ['UserId', 'Reputation', 'CreationDate', 'DisplayName',
        'LastAccessDate', 'WebsiteUrl', 'Location', 'AboutMe',
        'Views', 'UpVotes', 'DownVotes', 'AccountId'],
        axis='columns'
    )
    

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 2021-01-20
      相关资源
      最近更新 更多