I have successfully printed google spreadsheet data using python on google colaboratory:
- created project and generated json key for google doc app
- mounted google drive to google colab
- import spread via !pip install gspread google-auth
- printed the data from a google spreadsheet using JSON API key permissions from Google Cloud Platform
Next, I want to parse the data by forming a dictionary of characters and their associated coordinates: x,y. However, after printing the data from the spreadsheet successfully I failed to understand how to parse the data from gspread into a character dictionary. I used the following python code:
Preformatted text
def fetch_google_doc_data_google_doc_data(doc_url):
"""
Fetch data from a Google Sheet.
Assumes the document contains a table of characters and its coordinates.
"""
# Open the document by its URL
sh = client.open_by_url("https://docs.google.com/spreadsheets/d/1PhcQxtYIbabosVLp-SsPz4YfXM_NMoDv35H-1vkIaD4/")
# Get the first worksheet
sh = sheet.get_worksheet(0)
# Get all data from the worksheet
worksheet = sh.get_worksheet(0)
print(worksheet.get_all_values())
def parse_data(data):
"""
Parses the data into a dictionary mapping coordinates to Characters.
"""
char_dict = {}
# Parse each row in the data
for row in data[1:]: # Skip the header row
try:
# Assume each row is formatted as [Character, x, y]
char, x_strand, y_strand = row
x = int(x_strand)
y = int(y_strand)
char_dict[(x,y)] = char
except ValueError:
# Skip rows that don't match the expected format
pass
return char_dict
The code printed the entire spreadsheet data but the empty character dictionary fails to populate. Do I need to define data in parse the data segment of the code? i.e. def parse_data(data):