【发布时间】:2018-11-20 16:42:13
【问题描述】:
我是 python 编码的新手,我想从服务器获取 XML 文件,解析它并保存到 csv 文件。
2 部分没问题,我可以获取文件并对其进行解析,但另存为 csv 存在问题。
代码:
import requests
import numpy as np
hu = requests.get('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml', stream=True)
from xml.etree import ElementTree as ET
tree = ET.parse(hu.raw)
root = tree.getroot()
namespaces = {'ex': 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref'}
for cube in root.findall('.//ex:Cube[@currency]', namespaces=namespaces):
np.savetxt('data.csv', (cube.attrib['currency'], cube.attrib['rate']), delimiter=',')
我得到的错误是:数组 dtype 和格式说明符不匹配。 这可能意味着我获取数据并尝试将其保存为数组,但出现不匹配。 但我不确定如何解决问题并且不匹配。
谢谢
【问题讨论】: