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 读取文件,而不是下载它吗??
是的,标准
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
文件打开的,因为该函数无法知道它是否是文本文件。