我試圖將下面的打印命令輸出到字典中(沒有成功),以便隨後將其導出爲CSV。Python 3 - 將變量導入字典
我怎樣才能得到parseddata
(輸出下面的打印)到一個字典?
樣本輸入文件:
<html>
<body>
<p>{ success:true ,results:3,rows:[{ISIN:"INE134E01011",Ind:"-",Audited:"Un-Audited",Cumulative:"Non-cumulative",Consolidated:"Non-Consolidated",FilingDate:"14-Aug-2015 15:39",SeqNumber:"1001577"},{ISIN:"INE134E01011",Ind:"-",Audited:"Un-Audited",Cumulative:"Non-cumulative",Consolidated:"Non-Consolidated",FilingDate:"30-May-2015 14:37",SeqNumber:"129901"},{ISIN:"INE134E01011",Ind:"-",Audited:"Un-Audited",Cumulative:"Non-cumulative",Consolidated:"Non-Consolidated",FilingDate:"17-Feb-2015 14:57",SeqNumber:"126171"}]}</p>
</body>
</html>
我的代碼:
import requests
import re
from bs4 import BeautifulSoup
url = requests.get("http://. . .")
soup = BeautifulSoup(url.text, "lxml")
parseddata = soup.string.split(':[', 1)[1].lstrip(']')
print(parseddata)
的print(parseddata)
輸出爲:
{ISIN:"INE134E01011",Ind:"-",Audited:"Un-Audited",Cumulative:"Non-cumulative",Consolidated:"Non-Consolidated",FilingDate:"14-Aug-2015 15:39",SeqNumber:"1001577"},{ISIN:"INE134E01011",Ind:"-",Audited:"Un-Audited",Cumulative:"Non-cumulative",Consolidated:"Non-Consolidated",FilingDate:"30-May-2015 14:37",SeqNumber:"129901"},{ISIN:"INE134E01011",Ind:"-",Audited:"Un-Audited",Cumulative:"Non-cumulative",Consolidated:"Non-Consolidated",FilingDate:"17-Feb-2015 14:57",SeqNumber:"126171"}]}
但是'parseddata'看起來像什麼? – yurib
yurib,我已編輯帖子以顯示parseddata的樣子。謝謝 –
@zs_python:你能提供一個樣本輸入文件來處理,以便人們可以運行測試用例。 –