Attempting to open .txt file, read the contents of the file and append everything inside to a google spreadsheet. The exact contents of the file would be something exactly like [['65574','7657565','76576575','543533','543244634']]. Getting an error for Invalid value at 'data.values'. Here is what I've got right now. `
with open('numbers_list.txt') as f:
data = f.read()
values = data
sheets_data = {'values': values}
update_sheet = sheets_service.spreadsheets().values().append(
spreadsheetId=spreadsheet_id,
body=sheets_data,
range=range_place,
valueInputOption='USER_ENTERED'
).execute()
` To check if the contents of my file were not written correctly, I manually wrote them instead of reading them from the file to see if it would produce the same error "Invalid value at 'data.values'". Like this
`
values = [['65574','7657565','76576575','543533','543244634']]
sheets_data = {'values': values}
update_sheet = sheets_service.spreadsheets().values().append(
spreadsheetId=spreadsheet_id,
body=sheets_data,
range=range_place,
valueInputOption='USER_ENTERED'
).execute()
` This did not produce any error and worked perfectly fine. Unsure why an error is created when the values are the exact same. Appreciate any feedback, the full numbers list in actual practice will be much longer and not feasible to manually write.
I think it is because when you read the contents of the file, it returns a string, not a list. You can fix it with literal_eval()
function from ast
library.
For example
import ast
with open('number_list.txt') as f:
data = f.read()
values = ast.literal_eval(data)
This converts your stringified list into a list and should work properly now.