2017-10-05 111 views
0

我想直接從網站上讀取CSV文件(從可下載的鏈接),然後將其列作爲列表獲取,以便我可以進一步使用它。我無法正確編碼。最近的我已經達到了直接從Python網站讀取csv文件3

import csv 
import urllib.request as urllib 
import urllib.request as urlRequest 
import urllib.parse as urlParse 

url = "https://www.nseindia.com/content/indices/ind_nifty50list.csv" 
# pretend to be a chrome 47 browser on a windows 10 machine 
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"} 
req = urlRequest.Request(url, headers = headers) 
# open the url 
x = urlRequest.urlopen(req) 
sourceCode = x.read() 
+0

您在那裏先生! – Juggernaut

+0

看來您已經成功下載了csv。您需要使用Python文檔中描述的** csv **模塊來處理此文件。 –

回答

1

你很接近目標。

通過線只是拆讀CSV數據,並將其傳遞給csv.reader():

import csv 
import urllib.request as urllib 
import urllib.request as urlRequest 
import urllib.parse as urlParse 

url = "https://www.nseindia.com/content/indices/ind_nifty50list.csv" 
# pretend to be a chrome 47 browser on a windows 10 machine 
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"} 
req = urlRequest.Request(url, headers = headers) 
# open the url 
x = urlRequest.urlopen(req) 
sourceCode = x.read() 

cr = csv.DictReader(sourceCode.splitlines()) 
l = [row['Series'] for row in cr] 

但是請注意,x.read()回報bytearray,所以如果CSV包含非ASCII符號,不要忘了添加:

x.read().decode('utf-8') # or another encoding you need