簡體   English   中英

如何從谷歌電子表格中提取數據到筆記本

[英]How to extract data from a google spreadsheet to notebook

我想從這個每 5 分鍾更新一次的Google 電子表格中提取有關印度所有 CoVid19 病例的數據。 以前我曾從 API 中提取數據,但不知道如何從電子表格中提取數據。

關聯:

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

API 密鑰: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

參考:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM