Search code examples
python-3.xcsvdictionarydropbox-api

how to read csv file from dropbox as a dictionary (as read from csv.DictReader() )?


I'm trying to read a csv file from dropbox using

md, res = dbx.files_download(path)

as recommended in this link
This is the csv which I'm trying to read :csv_file_image
from res.content, I get data from csv file which is in ugly shape.
Is there any way to read data more in shape like using:

import csv
with open(file) as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row['name'])

I want to read csv file as a dictionary but all I get is the data as:response_data_image


Solution

  • I found this solution by converting res.content into iterable obj:

    import csv
    import dropbox
    
    path = '' # file path which is needed to be read
    dbx = dropbox.Dropbox('YOUR_ACCESS_TOKEN')
    md, res = dbx.files_download(path)
    data = res.content # data in ugly shape
    # Now data is needed to be decoded from bytes to unicode and then str.split()
    data = data.decode('utf-8')
    data = data.split('\n')
    # Now data is an iterable object, so csv.DictReader can be used
    reader = csv.DictReader(data)
    # Loop to get data using key value from reader dict
    for row in reader:
        row['nameen']    # 'nameen' is the key value in my case