import os
import json
import gspread
from oauth2client.service_account import ServiceAccountCredentials

# this file inspired by https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html

# use creds to create a client to interact with the Google Drive API
scopes = ['https://spreadsheets.google.com/feeds']
json_creds = os.getenv("GOOGLE_SHEETS_CREDS_JSON")
creds_dict = json.loads(json_creds)
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scopes)
client = gspread.authorize(creds)

# Find a workbook by url
spreadsheet = client.open_by_url("https://docs.google.com/spreadsheets/d/1RcQuetbKVYRRf0GhGZQi38okY8gT1cPUs6l3RM94yQo/edit#gid=704459328")
sheet = spreadsheet.sheet1

# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)
@@ -1,12 +1,23 @@
+import csv%0A
 import os%0Aim
@@ -14,16 +14,16 @@
 port os%0A
-
 import j
@@ -109,16 +109,19 @@
 ntials%0A%0A
+%0A%0A%0A
 # this f
@@ -413,16 +413,17 @@
 _JSON%22)%0A
+%0A
 creds_di
@@ -450,16 +450,93 @@
 _creds)%0A
+creds_dict%5B%22private_key%22%5D = creds_dict%5B%22private_key%22%5D.replace(%22%5C%5C%5C%5Cn%22, %22%5Cn%22)%0A
 creds = 
@@ -873,66 +873,1099 @@
 ues%0A
-list_of_hashes = sheet.get_all_records()%0Aprint(list_of_hashes
+rows = sheet.get_all_records()%0Aprint(rows)%0A%0A# import unicodecsv as csv%0A#%0A# with open('out.csv','wb') as f:%0A#     w = csv.DictWriter(f, fieldnames=sorted(rows%5B0%5D.keys()), encoding='utf-8-sig')%0A#     w.writeheader()%0A#     w.writerows(rows)%0A#%0A# # then do this https://cloud.google.com/bigquery/docs/loading-data-local%0A#%0A# from google.cloud import bigquery%0A# client = bigquery.Client()%0A# filename = 'out.csv'%0A# dataset_id = 'pmh'%0A# table_id = 'temp_rr'%0A#%0A# dataset_ref = client.dataset(dataset_id)%0A# table_ref = dataset_ref.table(table_id)%0A# job_config = bigquery.LoadJobConfig()%0A# job_config.source_format = bigquery.SourceFormat.CSV%0A# job_config.skip_leading_rows = 1%0A# job_config.autodetect = True%0A#%0A# with open(filename, 'rb') as source_file:%0A#     job = client.load_table_from_file(%0A#         source_file,%0A#         table_ref,%0A#         location='US',  # Must match the destination dataset location.%0A#         job_config=job_config)  # API request%0A#%0A# job.result()  # Waits for table load to complete.%0A#%0A# print('Loaded %7B%7D rows into %7B%7D:%7B%7D.'.format(%0A#     job.output_rows, dataset_id, table_id)
 )

handle bad escaping