2017-09-07 22 views
0

我想讓這個python腳本在這個github上工作。從URL中檢索數據,但收到錯誤:迭代器應該返回字符串,而不是字節

https://gist.github.com/lebedov/f09030b865c4cb142af1

腳本從谷歌獲取股票報價。

當我運行的功能stock = get_google_finance_intraday("AAPL"),我得到的錯誤_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

我追查到導致錯誤的代碼行。

這是行39

reader = csv.reader(page.content.splitlines()) 

我使用Python 3.6版。我懷疑代碼在python v2上運行,但不是v3。

回答

1

巨蟒-3需要你的字節數組(數據來自文件)解碼成實際的字符串

data = data.decode('utf8') 

你要做到這一點的線將它們傳遞到CSV解析器前拉。

相關問題