简体   繁体   中英

How to extract data from a google spreadsheet to notebook

I want to extract the data regarding all the CoVid19 cases in India from this Google Spreadsheet which updates every 5 min. previously i have worked with extracting data from APIs but have no idea how to do it from spreadsheets.

link:

https://docs.google.com/spreadsheets/d/1cB7QX00wceTIrEYix5q3eZfWepAbr4C_oTT6wDth-IA/edit?usp=sharing

API Key: you_need_your_own_API_key

#import library
import os
cwd = os.getcwd()
cwd

import gspread
#Service client credential from oauth2client
from oauth2client.service_account import ServiceAccountCredentials
# Print nicely
import pprint
#Create scope
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
#create some credential using that scope and content of startup_funding.json
creds = ServiceAccountCredentials.from_json_keyfile_name('startup_funding.json',scope)
#create gspread authorize using that credential
client = gspread.authorize(creds)
#Now will can access our google sheets we call client.open on StartupName
sheet = client.open('Test_Sheet').sheet1
pp = pprint.PrettyPrinter()
results = sheet.get_all_records()
results


Final Result:

[{'Name': 'Kim K.', 'Address': '10 Main St.', 'Age': 22},
 {'Name': 'Ralph G.', 'Address': '20 Killmore La.', 'Age': 56},
 {'Name': 'Timmy H.', 'Address': '56 Freemont Pl.', 'Age': 32},
 {'Name': 'Fred A.', 'Address': '71 South Terr.', 'Age': 41},
 {'Name': 'Danny S.', 'Address': '99 Penny La', 'Age': 62}]


Or, more specific ranges:
result = sheet.row_values(1) #See individual row
result
result = sheet.col_values(1) #See individual column
result
result = sheet.cell(5,2) # See particular cell
result

References:

https://medium.com/datadriveninvestor/use-google-sheets-as-your-database-using-python-77d40009860f

https://gspread.readthedocs.io/en/latest/user-guide.html#creating-a-spreadsheet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM