【问题标题】:How to construct a dictionary from XML elements using ElementTree?如何使用 ElementTree 从 XML 元素构造字典?
【发布时间】:2019-03-07 06:12:17
【问题描述】:

我的代码如下所示。它从这里提取 XML:https://www.sec.gov/Archives/edgar/data/1413909/000149315218018055/dsgt-20180930.xml

我想根据'xbrli:xbrl' 中的键和值创建字典 - 即根据下面第二个代码块中显示的键和值创建字典。

但是,我的代码返回一个空字典。它完全跳过xbrli:xbrl,直接转到link:schemaRef

import requests
import pandas as pd
import urllib.request  as urllib2
import xml.etree.ElementTree as ET
from lxml import etree

def namespaces(url):
    tree = ET.parse(urllib2.urlopen(url))
    root = tree.getroot()
    d = dict(root.attrib)
    return d.keys()

我想从这里创建一个字典:

<xbrli:xbrl
  xmlns:xbrli="http://www.xbrl.org/2003/instance"
  xmlns:DSGT="http://dsgtag.com/20180930"
  xmlns:country="http://xbrl.sec.gov/country/2017-01-31"
  xmlns:currency="http://xbrl.sec.gov/currency/2017-01-31"
  xmlns:dei="http://xbrl.sec.gov/dei/2018-01-31"
  xmlns:iso4217="http://www.xbrl.org/2003/iso4217"
  xmlns:link="http://www.xbrl.org/2003/linkbase"
  xmlns:nonnum="http://www.xbrl.org/dtr/type/non-numeric"
  xmlns:num="http://www.xbrl.org/dtr/type/numeric"
  xmlns:ref="http://www.xbrl.org/2006/ref"
  xmlns:srt="http://fasb.org/srt/2018-01-31"
  xmlns:us-gaap="http://fasb.org/us-gaap/2018-01-31"
  xmlns:us-roles="http://fasb.org/us-roles/2018-01-31"
  xmlns:xbrldi="http://xbrl.org/2006/xbrldi"
  xmlns:xbrldt="http://xbrl.org/2005/xbrldt"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>...</xbrli:xbrl>

【问题讨论】:

  • xml和dict()之间没有“直接转换”,无法智能检测,需要知道布局,预期输出,然后扩展,不支持“无尽层/未知数据类型”
  • 你可能需要查看docs.python.org/2/library/xml.etree.elementtree.html 谈论命名空间
  • 你的问题似乎和stackoverflow.com/q/42320779/407651中的一样
  • 您发布的样本不是字典。如果你想有一个 dict 作为输出 - 发布 dict 结构。示例:{'key':'value'}
  • 我猜你想自动创建一个命名空间 URI 和前缀的字典,所以可以在你的 XPath 表达式中使用它们,但你不必对它们进行硬编码?这不是一个好主意。命名空间 URI 是硬编码的。

标签: python xml elementtree


【解决方案1】:

解决方案基于ETiterparse

from io import StringIO
import xml.etree.ElementTree as ET
import requests
from pprint import pprint

r = requests.get('https://www.sec.gov/Archives/edgar/data/1413909/000149315218018055/dsgt-20180930.xml')
if r.status_code == 200:
    xml_data = unicode(r.content, "utf-8")
    document_namespaces = dict([node for _, node in ET.iterparse(StringIO(xml_data), events=['start-ns'])])
    pprint(document_namespaces)

输出

{u'DSGT': 'http://dsgtag.com/20180930',
 u'country': 'http://xbrl.sec.gov/country/2017-01-31',
 u'currency': 'http://xbrl.sec.gov/currency/2017-01-31',
 u'dei': 'http://xbrl.sec.gov/dei/2018-01-31',
 u'iso4217': 'http://www.xbrl.org/2003/iso4217',
 u'link': 'http://www.xbrl.org/2003/linkbase',
 u'nonnum': 'http://www.xbrl.org/dtr/type/non-numeric',
 u'num': 'http://www.xbrl.org/dtr/type/numeric',
 u'ref': 'http://www.xbrl.org/2006/ref',
 u'srt': 'http://fasb.org/srt/2018-01-31',
 u'us-gaap': 'http://fasb.org/us-gaap/2018-01-31',
 u'us-roles': 'http://fasb.org/us-roles/2018-01-31',
 u'xbrldi': 'http://xbrl.org/2006/xbrldi',
 u'xbrldt': 'http://xbrl.org/2005/xbrldt',
 u'xbrli': 'http://www.xbrl.org/2003/instance',
 u'xlink': 'http://www.w3.org/1999/xlink',
 u'xsi': 'http://www.w3.org/2001/XMLSchema-instance'}

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 2013-01-04
    • 2019-06-17
    • 2013-05-01
    • 2016-01-20
    • 1970-01-01
    相关资源
    最近更新 更多