1. 程式人生 > 實用技巧 >python 從url中獲取csv或npy內容 (python get the content of csv or npy from url)

python 從url中獲取csv或npy內容 (python get the content of csv or npy from url)

python 從 url 網址中獲取 csv 和 npy 檔案內容程式碼:

 1 import requests
 2 import numpy as np
 3 from urllib.request import urlopen
 4 import io
 5 
 6 
 7 def url_csv_to_array(url):
 8     response = urlopen(url)
 9     url_content = response.read()
10     url_content_decode = url_content.decode()
11     url_content_list = url_content_decode.split("
\n") 12 url_data = [] 13 for row in url_content_list[:-1]: 14 row_split = row.split(",") 15 url_data.append([int(i) for i in row_split]) 16 url_data_arry = np.array(url_data, dtype=np.int16) 17 return url_data_arry 18 19 20 url_csv = "http://.../point.csv" 21 url_data_csv = url_csv_to_array(url_csv)
22 23 24 def url_read_npy(url_npy): 25 response = requests.get(url_npy) 26 # 判斷返回的Response型別狀態是不是200。如果是200,他將表示返回的內容是正確的,如果不是200,他就會產生一個HttpError的異常。 27 response.raise_for_status() 28 npy_data = np.load(io.BytesIO(response.content)) 29 return npy_data 30 31 32 url_npy = "http://.../B004_tr.npy
" 33 url_data_npy = url_read_npy(url_npy)

參考資料:

https://stackoverflow.com/questions/16283799/how-to-read-a-csv-file-from-a-url-with-python

https://stackoverflow.com/questions/52884563/loading-numpy-array-from-http-response-without-saving-a-file/61716809#61716809

中途遇到了需要使用urllib2的時候,後來發現一直安裝不上,查了用了以下解決方法:

https://stackoverflow.com/questions/2792650/import-error-no-module-name-urllib2

結果如下所示: