itChi
itChi

Reputation: 662

pygsheets importing csv into worksheet

Having trouble getting pygsheets to accept the list format for reading CSV's

import pygsheets
import csv

gc = pygsheets.authorize(service_file=service_account_file)
# Open spreadsheet
sh = gc.open_by_key(spreadsheet_key)
# Open Worksheet
wks = sh.worksheet_by_title(spreadsheet_hosts_worksheet)

with open('sheets-data/Sheets-Hosts-Export.csv', 'r') as f:
  reader = csv.reader(f, skipinitialspace=True, delimiter=',', quotechar='"')
  data = list(reader)

wks.update_values(cell_list=data)

However the output I get is:

Traceback (most recent call last):
  File "./push-sheets-hosts-data.py", line 47, in <module>
    wks.update_values(cell_list=data)
  File "/usr/local/lib/python3.5/dist-packages/pygsheets/worksheet.py", line 573, in update_values
    tmp_row.append(cell_list[row][col].value)
TypeError: list indices must be integers or slices, not list

But the source shows that It's checking for a list? Where am I going wrong please? source

First and second fields just for ref: from

print(data[0])
print(data[1])

['host_name', 'alias', 'address', 'parents', 'use', 'display_name', 'hostgroups', 'contacts', '_ADDINFO', '_SNOWGROUP', '_RTTCRIT', '_RTTWARN', 'contact_groups', 'notes', 'notes_url', 'check_command', 'first_notification_delay', 'check_interval', 'max_check_attempts', 'retry_interval', 'config_filename']
['host', 'destiny islands', '1.1.1.1', 'host.mypalace.com,host.mapalace2.com', 'tmpl_network_device', 'Gingerbread lane', 'hgrp_grandad', '', 'ACTION - Don't forget to smile', 'test', '70', '20', '', '', '', '', '', '', '', '', '/folder/filename']

Upvotes: 2

Views: 1279

Answers (1)

Tanaike
Tanaike

Reputation: 201378

How about this modification?

Modification points:

  • Please use values instead of cell_list.
  • Please include the range you want to put the value.

Modified script:

Please modify your script as follows.

From:
wks.update_values(cell_list=data)
To:
wks.update_values(crange='A1', values=data)

or

wks.update_values('A1', data)

Note:

  • In this case, the values are put from the cell "A1". For example, when "C3" is given as the range, the values are put from "C3".

Reference:

If I misunderstood your situation, I apologize.

Upvotes: 1

Related Questions