[英]Pandas read_csv response codes when using an external url
I'm replacing requests.get()
with pd.read_csv()
and would like to write some exception logic if pandas does not get the equivalent of a status code 200.我正在用pd.read_csv()
替换requests.get()
,如果 pandas 没有获得相当于状态代码 200 的值,我想编写一些异常逻辑。
With requests, I can write:有了请求,我可以写:
response = requests.get(report_url)
if response.status_code != 200:
How can I apply the same logic to pd.read_csv()
?如何将相同的逻辑应用于pd.read_csv()
? Are there any status codes I can check on?我可以检查任何状态代码吗?
My suggestion is to write a custom reader that makes it possible to check that a URL is valid before reading it although this defeats the purpose我的建议是编写一个自定义阅读器,可以在阅读之前检查 URL 是否有效,尽管这违背了目的
import requests
def custom_read(url):
try:
return_file = pd.read_csv(url)
except requests.exceptions.HTTPError as err:
raise
else:
return return_file
A valid URL will work有效的 URL 将起作用
my_file = custom_read("https://people.sc.fsu.edu/~jburkardt/data/csv/addresses.csv")
This fails and raises a requests
error这失败并引发requests
错误
my_file1 = custom_read("https://uhoh.com")
Otherwise, there is no way to access the status code of a URL for a DataFrame
object once it has been read.否则,一旦读取DataFrame
对象,就无法访问其 URL 的状态代码。
You can use url
in read_csv()
but it has no method to gives you status code.您可以在read_csv()
中使用url
,但它无法为您提供状态代码。 It simply raises error when it has non-200 status code and you have to use try/except
to catch it.当它具有非 200 状态代码并且您必须使用try/except
来捕获它时,它只会引发错误。 You have example in other answer.您在其他答案中有示例。
But if you have to use requests
then you can later use io.StringIO
to create file-like object (file in memory) and use it in read_csv()
.但是,如果您必须使用requests
,那么您可以稍后使用io.StringIO
创建类似文件的对象(内存中的文件)并在read_csv()
中使用它。
import io
import requests
import pandas as pd
response = requests.get("https://people.sc.fsu.edu/~jburkardt/data/csv/addresses.csv")
print('status_code:', response.status_code)
#if response.status_code == 200:
if response.ok:
df = pd.read_csv( io.StringIO(response.text) )
else:
df = None
print(df)
The same way you can use io.StringIO
when you create web page which gets csv
using HTML
with <form>
.与使用带有<form>
的HTML
创建获取csv
的网页时可以使用io.StringIO
的方式相同。
As I know read_csv(url)
works in similar way - it uses requests.get()
to get file data from server and later it uses io.StringIO
to read data.据我所知read_csv(url)
以类似的方式工作 - 它使用requests.get()
从服务器获取文件数据,然后使用io.StringIO
读取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.