Search code examples
pythonpandasexport-to-csvdropboxdropbox-api

Unable to write a CSV into Dropbox APP V2 - CSV appears blank


I'm trying to upload my script's output (pandas df) into a Dropbox folder, but the csv file created in Dropbox appears blank.

dbx.files_upload(cvd_tracker.to_csv("Trial.csv"),'/Trial.csv')

I'm guessing the .to_csv() doesn't apply here, but I couldn't figure out how to do it. I've checked, and the "cvd_tracker" dataframe does have data in it.

Using the StringIO also failed.

from io import StringIO
csv_buffer = StringIO()
cvd_tracker.to_csv(csv_buffer)

dbx.files_upload(csv_buffer.getvalue(),'/Trial3.csv')

enter image description here


Solution

  • dbx.files_upload has two required parameters, the first is a file-like object or a bytes-like object, the second is a path in the user's dropbox in which to save the file. Your second parameter looks ok, but your first parameter is incorrect - pandas.Dataframe.to_csv returns a string if the first parameter passed to it is None, or it returns None if the first parameter is not None. The first (and only) parameter you pass to cvd_tracker.to_csv is "Trial.csv", so cvd_tracker.to_csv("Trial.csv") returns None. This means essentially you are doing dbx.files_upload(None, "/Trial.csv").

    To get the desired result, simply remove the argument passed to cvd_tracker.to_csv, so that it returns a string. You then need to turn the string into a bytes-like object by using str.encode, so that dbx.files_upload can accept it. It would look like this:

    dbx.files_upload(cvd_tracker.to_csv().encode(), "/Trial.csv")