Search code examples
python-3.xcsvdropboxdropbox-api

Restructure data loaded from dropbox file in python


I am trying to download data from a CSV file stored in a dropbox folder. So far I do like this:

import dropbox

#Get access to my dropbox folder
dbx = dropbox.Dropbox('SOME_ACCESS_TOKEN')
dbx.users_get_current_account()

#Download file    
metadata, res = dbx.files_download('/Test.csv')

#Get the file content
data=res.content
print(data)

data is of the this form: b'1,2,3,4,5\r\nA,B,C,D,E\r\n1,2,3,4,5\r\nA,B,C,D,E\r\n1,2,3,4,5\r\nA,B,C,D,E\r\n'

Is there an easy way to restructure this into a list of lists?


Solution

  • The solution to the above mentioned problem is:

    import dropbox
    
    #Connect to dropbox folder
    dbx = dropbox.Dropbox('SOME_ACCESS_TOKEN')
    dbx.users_get_current_account()
    
    #Get metadata
    metadata, res = dbx.files_download('/Test.txt')
    
    #Get and decode data
    data=res.content.decode('utf-8')
    
    #Restructure data
    lines = data.split('\r\n')
    lines.pop()
    
    print(lines)