Skip to content

Instantly share code, notes, and snippets.

@ThePyProgrammer
Created September 14, 2021 09:01
Show Gist options
  • Save ThePyProgrammer/5c3c5916b97acd3acdfb2d134fb5ef18 to your computer and use it in GitHub Desktop.
Save ThePyProgrammer/5c3c5916b97acd3acdfb2d134fb5ef18 to your computer and use it in GitHub Desktop.
A utitlity function list that can allow simpler code.
import requests
import pandas as pd
import re
from io import StringIO
def read_csv(loc, *args, **kwargs):
if re.search(r"[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)", loc):
return read_csv(requests.get(loc, allow_redirects=True).content, *args, **kwargs)
loc = loc.strip("\n")
if re.search(r"[\n:<>\"/\|?*]", loc):
return pd.read_csv(StringIO(loc), *args, **kwargs)
else:
return pd.read_csv(loc, *args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment