You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从XML响应的tabellenDaten标签提取CSV并转为Pandas DataFrame

提取XML响应中的CSV数据并转换为Pandas DataFrame

步骤说明

  1. 修正请求方式:SOAP接口通常要求使用POST请求而非GET,原代码的请求方法需要调整。
  2. 解析XML响应:用Python内置的xml.etree.ElementTree解析返回的XML内容,定位到<tabellenDaten>标签提取其中的CSV文本。
  3. 读取CSV为DataFrame:将提取到的CSV文本通过io.StringIO转换为可读取的流对象,再用Pandas的read_csv方法加载,注意德语CSV常用分号;作为分隔符。

完整代码示例

import requests
import xml.etree.ElementTree as ET
import pandas as pd
from io import StringIO

# 简化请求URL(GET参数已包含在SOAP payload中)
url = "https://www-genesis.destatis.de/genesisWS/web/ExportService_2010"

payload = """<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://webservice_2010.genesis">
 <soapenv:Header/>
 <soapenv:Body>
 <web:TabellenExport soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 <kennung xsi:type="xsd:string">DEB924AL95</kennung>
 <passwort xsi:type="xsd:string">P@ssword123</passwort>
 <namen xsi:type="xsd:string">42151-0002</namen>
 <bereich xsi:type="xsd:string">alle</bereich>
 <format xsi:type="xsd:string">csv</format>
 <strukturinformation xsi:type="xsd:boolean">false</strukturinformation>
 <komprimieren xsi:type="xsd:boolean">false</komprimieren>
 <transponieren xsi:type="xsd:boolean">false</transponieren>

 <startjahr xsi:type="xsd:string"></startjahr>
 <endjahr xsi:type="xsd:string"></endjahr>
 <zeitscheiben xsi:type="xsd:string"></zeitscheiben>
 <regionalmerkmal xsi:type="xsd:string"></regionalmerkmal>
 <regionalschluessel xsi:type="xsd:string"></regionalschluessel>
 <sachmerkmal xsi:type="xsd:string">FAMSTD</sachmerkmal>
 <sachschluessel xsi:type="xsd:string">VERH</sachschluessel>
 <sachmerkmal2 xsi:type="xsd:string"></sachmerkmal2>
 <sachschluessel2 xsi:type="xsd:string"></sachschluessel2>
 <sachmerkmal3 xsi:type="xsd:string"></sachmerkmal3>
 <sachschluessel3 xsi:type="xsd:string"></sachschluessel3>
 <stand xsi:type="xsd:string"></stand>
 <auftrag xsi:type="xsd:boolean">false</auftrag>
 <sprache xsi:type="xsd:string">de</sprache>
 </web:TabellenExport>
 </soapenv:Body>
</soapenv:Envelope>"""

headers = {
    'Content-Type': 'application/xml'
}

# 发送POST请求
response = requests.request("POST", url, headers=headers, data=payload)

# 解析XML响应,处理命名空间
root = ET.fromstring(response.content)
namespace = {'web': 'http://webservice_2010.genesis'}
csv_content = root.find('.//web:tabellenDaten', namespace).text

# 将CSV文本转为DataFrame(可根据实际分隔符调整sep参数)
df = pd.read_csv(StringIO(csv_content), sep=';')

# 查看数据前几行
print(df.head())

关键细节说明

  • 命名空间处理:XML响应中的标签带有命名空间,必须指定namespace字典才能正确定位<tabellenDaten>标签。
  • 分隔符调整:如果返回的CSV使用逗号作为分隔符,将sep=';'改为sep=','即可。
  • URL简化:原URL中的GET参数已在SOAP payload中定义,简化URL可避免参数冲突。

内容的提问来源于stack exchange,提问作者prashanth manohar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 09:07:55