url = 'https://cdn.wsform.com/wp-content/uploads/2021/05/currency.csv'
f = open(url, 'r')
print(f.read())
Invalid argument: 'https://cdn.wsform.com/wp-content/uploads/2021/05/currency.csv'
有什么辦法可以讓我從 url 讀取檔案,而不是下載它?
uj5u.com熱心網友回復:
是的,標準urllib.request.urlopen()正是這樣做的:
from urllib import request
with request.urlopen('https://cdn.wsform.com/wp-content/uploads/2021/05/currency.csv') as f:
print(f.read().decode()) # the decode turns the bytes into a string for printing
請注意,該檔案是作為 的檔案打開的bytes,因為該函式無法知道它是否是文本檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/489602.html
上一篇:Pythoncsv檔案-打開一個csv檔案并從2列中獲取所有資訊,獲取唯一值,然后洗掉一些回復(沒有Pandas)
