2017-11-10 74 views
0

我想在將數據填入數據庫表之前將一些XML放入熊貓數據框中。我已經看過元素樹和lxml,但這些例子非常簡單,我似乎無法將它們推斷爲複雜的東西。我瞭解XML我只是不知道如何深入到我需要的東西。下面是一個示例。從Python中的ADO.Net數據服務XML解析元數據屬性

我在<m:properties>的東西之後。所以NEW_DATE = 1997-01-02T00:00:00,BC_1YEAR = 5.630000114440918等等是數據庫中的內容。請注意BC_1MONTH = NULL,並且不像其他節點。

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<feed xml:base="http://data.treasury.gov/Feed.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> 
    <title type="text">DailyTreasuryYieldCurveRateData</title> 
    <id>http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData</id> 
    <updated>2017-10-30T20:31:53Z</updated> 
    <link rel="self" title="DailyTreasuryYieldCurveRateData" href="DailyTreasuryYieldCurveRateData" /> 
    <entry> 
    <id>http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(1)</id> 
    <title type="text"></title> 
    <updated>2017-10-30T20:31:53Z</updated> 
    <author> 
     <name /> 
    </author> 
    <link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(1)" /> 
    <category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
    <content type="application/xml"> 
     <m:properties> 
     <d:Id m:type="Edm.Int32">1</d:Id> 
     <d:NEW_DATE m:type="Edm.DateTime">1997-01-02T00:00:00</d:NEW_DATE> 
     <d:BC_1MONTH m:type="Edm.Double" m:null="true" /> 
     <d:BC_3MONTH m:type="Edm.Double">5.190000057220459</d:BC_3MONTH> 
     <d:BC_6MONTH m:type="Edm.Double">5.3499999046325684</d:BC_6MONTH> 
     <d:BC_1YEAR m:type="Edm.Double">5.630000114440918</d:BC_1YEAR> 
     <d:BC_2YEAR m:type="Edm.Double">5.96999979019165</d:BC_2YEAR> 
     <d:BC_3YEAR m:type="Edm.Double">6.130000114440918</d:BC_3YEAR> 
     <d:BC_5YEAR m:type="Edm.Double">6.3000001907348633</d:BC_5YEAR> 
     <d:BC_7YEAR m:type="Edm.Double">6.4499998092651367</d:BC_7YEAR> 
     <d:BC_10YEAR m:type="Edm.Double">6.5399999618530273</d:BC_10YEAR> 
     <d:BC_20YEAR m:type="Edm.Double">6.8499999046325684</d:BC_20YEAR> 
     <d:BC_30YEAR m:type="Edm.Double">6.75</d:BC_30YEAR> 
     <d:BC_30YEARDISPLAY m:type="Edm.Double">0</d:BC_30YEARDISPLAY> 
     </m:properties> 
    </content> 
    </entry> 
</feed> 

如果您有關於此的一篇好文章的鏈接,那也將不勝感激。

下面是我的工作代碼:從達菲的代碼收到

import xml.etree.ElementTree as ET 
import pandas as pd 

xml_data = open('/path/user_agents.xml').read() 

def xml2df(xml_data): 
    root = ET.XML(xml_data) # element tree 
    all_records = [] 
    for i, child in enumerate(root): 
     record = {} 
     for subchild in child: 
      record[subchild.tag] = subchild.text 
      all_records.append(record) 
    return pd.DataFrame(all_records) 

錯誤消息:

Traceback (most recent call last): 
    File "C:/Users/Bob/Desktop/temp/yield curve script.py", line 25, in <module> 
    xml2dict(xml_data) 
    File "C:/Users/Bob/Desktop/temp/yield curve script.py", line 13, in xml2dict 
    root = lxml.etree.parse(xml_file) 
    File "src\lxml\lxml.etree.pyx", line 3427, in lxml.etree.parse (src\lxml\lxml.etree.c:81100) 
    File "src\lxml\parser.pxi", line 1811, in lxml.etree._parseDocument (src\lxml\lxml.etree.c:117831) 
    File "src\lxml\parser.pxi", line 1837, in lxml.etree._parseDocumentFromURL (src\lxml\lxml.etree.c:118178) 
    File "src\lxml\parser.pxi", line 1741, in lxml.etree._parseDocFromFile (src\lxml\lxml.etree.c:117090) 
    File "src\lxml\parser.pxi", line 1138, in lxml.etree._BaseParser._parseDocFromFile (src\lxml\lxml.etree.c:111636) 
    File "src\lxml\parser.pxi", line 595, in lxml.etree._ParserContext._handleParseResultDoc (src\lxml\lxml.etree.c:105092) 
    File "src\lxml\parser.pxi", line 706, in lxml.etree._handleParseResult (src\lxml\lxml.etree.c:106800) 
    File "src\lxml\parser.pxi", line 633, in lxml.etree._raiseParseError (src\lxml\lxml.etree.c:105611) 
OSError: Error reading file '<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
+0

你想使用上游ElementTree多少錢?取而代之的是,[lxml.etree](http://lxml.de/tutorial.html)會使這些變得容易得多。 –

+0

獲取'// {http://schemas.microsoft.com/ado/2007/08/dataservices/metadata}屬性'並迭代子節點。 –

+0

我打算把'{'m':'http://schemas.microsoft.com/ado/2007/08/dataservices/met adata'}'作爲nsmap傳遞,但這也起作用。 :) –

回答

1
import lxml.etree 
import datetime 

nsmap = { 
    'm': 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata', 
    'd': 'http://schemas.microsoft.com/ado/2007/08/dataservices' 
} 
m_null = ('{%s}null' % nsmap['m']) 
m_type = ('{%s}type' % nsmap['m']) 

type_handlers = { 
    'Edm.Double': float, 
    'Edm.Int32': int, 
    'Edm.DateTime': lambda s: datetime.datetime.strptime(s.translate(None, ':-'), "%Y%m%dT%H%M%S"), 
} 

def xml2dict(xml_file): 
    root = lxml.etree.parse(xml_file) 
    result = {} 
    for properties_el in root.xpath('//m:properties', namespaces=nsmap): 
     for child in properties_el.getchildren(): 
      tag = child.tag.split('}',1)[-1] # split the namespace off the tag 
      if child.attrib.get(m_null): 
       value = None 
      else: 
       value = child.text 
       type_handler = type_handlers.get(child.attrib.get(m_type)) 
       if type_handler is not None: 
        value = type_handler(value) 
      result[tag] = value 
    return result 

...正常返回,爲您的數據:

{'BC_10YEAR': 6.539999961853027, 
'BC_1MONTH': None, 
'BC_1YEAR': 5.630000114440918, 
'BC_20YEAR': 6.849999904632568, 
'BC_2YEAR': 5.96999979019165, 
'BC_30YEAR': 6.75, 
'BC_30YEARDISPLAY': 0.0, 
'BC_3MONTH': 5.190000057220459, 
'BC_3YEAR': 6.130000114440918, 
'BC_5YEAR': 6.300000190734863, 
'BC_6MONTH': 5.349999904632568, 
'BC_7YEAR': 6.449999809265137, 
'Id': 1, 
'NEW_DATE': datetime.datetime(1997, 1, 2, 0, 0)} 
+0

東西不太對。 'root = lxml.etree.parse(xml_file)'我得到一個消息,說「在導入的模塊lxml中找不到引用'etree'。我厭倦了pip安裝lxml,但它說它已經安裝。 ,它出錯了,我發佈的文件是文件的一部分,儘管我可以告訴它它是格式良好的XML,下面是整個文件https://www.treasury.gov/resource-center/data-chart-center/利率/ Pages/TextView.aspx?data = yield選擇時間段= ALL在選擇列表底部 –

+0

'import lxml.etree',不只是'import lxml'。 –

+0

是的,我得到了正確的import語句。我只是通過發佈的XML來運行它,但也失敗了。這是在Python 3中,如果這有所幫助,我將用錯誤消息更新問題。 –