How to import a CSV file using Google Sheets API V4

前端 未结 5 1764
独厮守ぢ
独厮守ぢ 2020-12-30 06:14

Background

I\'m developing a Python 2.7 script that analyzes data from an SQL table and at the end, generates a CSV file.

Once the file is gener

5条回答
  •  无人及你
    2020-12-30 07:00

    An alternative to Sam Berlin's answer, you can turn your CSV into a list of lists and set that to your POST payload.

    Such a function looks something like this:

    def preprocess(table):
        table.to_csv('pivoted.csv') # I use Pandas but use whatever you'd like
        _file = open('pivoted.csv')
        contents = _file.read()
        array = contents.split('\n')
        master_array = []
        for row in array:
            master_array.append(row.split(','))
        return master_array
    

    That master array gets thrown into the following:

    body = {
          'values': newValues
    }
    
        result2 = service.spreadsheets().values().update(spreadsheetId=spreadsheetId, range=rangeName + str(len(values) + start + 1), valueInputOption="USER_ENTERED", body=body).execute()
    

    It works just fine for me.

提交回复
热议问题