我已經從網上下載的在線網頁XML格式的數據集。我已經使用python的模式庫提取了值標記。這給了我unicode值。無法轉換爲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:
這似乎是爲我工作... http://repl.it/LUj – Lix
'浮動(u'63.3' )'不給錯誤。 – user278064
如果你想有一個浮動的,爲什麼你會嘗試'INT()'? – unwind