Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

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()? Are there any status codes I can check on?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use url in read_csv() but it has no method to gives you status code. It simply raises error when it has non-200 status code and you have to use try/except to catch it. 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().

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>.


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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading