我从在线网页下载了一个 xml 格式的数据集。我已经使用 python 的模式库提取了值标签。这给了我 unicode 值。
我希望将此 unicode 数字(如 u'63.3')转换为浮点数。我试过做 float(a) 但它给出了错误消息。
import numpy as np
import pandas as pd
import requests
from pattern import web
xml = requests.get('http://charts.realclearpolitics.com/charts/1044.xml').text
dom = web.Element(xml)
values = dom.by_tag('value')
date= []
approve = []
disapprove = []
values = dom.by_tag('value')
for i in range(0,1724):
date.append(pd.to_datetime(values[i].content))
for i in range(1724,3448):
a = float(values[i].content)
approve.append(a)
for i in range(3448,5172):
a = float(values[i].content)
disapprove.append(a)
finalresult = pd.DataFrame({'date': date, 'Approve': approve, 'Disapprove': disapprove})
finalresult
输出如下:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-12-d9d9e580d883> in <module>()
9
10 for i in range(1724,3448):
---> 11 a = float(values[i].content)
12 result['Approve'].append(a)
13
ValueError: could not convert string to float: