Created
January 5, 2022 15:20
-
-
Save marksparrish/2d1af59da33d5b8c455e1f4fe45efe50 to your computer and use it in GitHub Desktop.
Dropbox Zipfile CSVs to pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import zipfile | |
import dropbox | |
import pandas as pd | |
def get_dropbox(): | |
dropbox_path = "path/to/dropbox/files" | |
dbx = dropbox.Dropbox('DROPBOX_KEY') | |
# get listing of files | |
dbx_files = dbx.files_list_folder(dropbox_path) | |
# sort to get the latest file | |
dbx_files.entries.sort(key = lambda d: d.client_modified, reverse=True) | |
df = pd.DataFrame() | |
_, res = dbx.files_download(dbx_files.entries[0].path_lower) | |
with io.BytesIO(res.content) as stream: | |
zf = zipfile.ZipFile(stream) | |
for f in zf.namelist(): | |
try: | |
dfs = pd.read_csv(zf.open(f), sep="\t", dtype=str, low_memory=False, encoding_errors='ignore') | |
df = df.append(dfs, ignore_index=True) | |
except ValueError: | |
pass | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment