Ame*_*men 2 google-sheets python-3.x google-sheets-api
我编写了一些代码来收集当地企业名称、邮政编码、地址和电话号码。现在我想按照我想要的顺序将它们写入我的谷歌电子表格的下一行。我对 gsheets API 的使用很陌生。谁能解释一下我们如何才能做到这一点?
我有一个字典列表,其中每个字典将被写入谷歌表中的下一行。
例如,假设我们有这个列表,为了简单起见,我只使用了一本字典,但在实际代码中,我将拥有多个字典:
businesses = [{name: 'Coffee_Shop', post_code: 'E6 7HJ', 'address1': '87 Longbridge Road', phone: '0773827354'}]
Run Code Online (Sandbox Code Playgroud)
更新:
使用 gspread 方法时出现此错误,如下所示:
AttributeError: 'str' object has no attribute '__module__'
这是整个脚本:
import csv
import os.path
import gspread
from requests.api import post
import config
#User_Interact
zip_code = input('Please enter a ZIP Code. ')
term = input('What do you want to search for near {0}? '.format(zip_code))
#initalisations
business_to_be_made = []
bus = []
final = []
url = 'https://api.yelp.com/v3/businesses/search'
headers = {
'Authorization':'Bearer ' + config.api_key
}
#The api_key must be generated by each user.
params = {
'term': term,
"location": zip_code
}
#contacts yelp api for data
response = requests.get(url, headers=headers, params=params)
total_response = response.json()
business_list = total_response['businesses']
#makes list of all businesses in the vicinity by type
for item in business_list:
location = item['location']
business_to_be_made.append({'name': item['name'],'zip_code': location['zip_code'], 'address': location['address1'], 'phone_number': item['phone'] })
sam_postcodes = open('file.txt', 'r')
postcodes_final = []
for item in sam_postcodes:
postcodes_final.append(item)
global final_list
final_list = []
for business in business_to_be_made:
if business['zip_code'] in postcodes_final:
final_list.append(business)
else:
pass
credentials = 'API_KEY_HIDDEN'
client = gspread.authorize(credentials) # Please use this line for your script.
spreadsheet_id = str('SPREADHSEET_ID_HIDDEN') # Please set the Spreadsheet ID.
sheet_name = str('Sheet18') # Please set the sheet name.
order = ['name', 'post_code', 'address1', 'phone'] # This is the order you want to put the values.
values = [[o[e] for e in order] for o in final_list]
spreadsheet = client.open_by_key(spreadsheet_id)
sheet = spreadsheet.worksheet(sheet_name)
sheet.append_rows(values, value_input_option='USER_ENTERED')
Run Code Online (Sandbox Code Playgroud)
我相信您的目标如下。
businesses = [{name: 'Coffee_Shop', post_code: 'E6 7HJ', 'address1': '87 Longbridge Road', phone: '0773827354'}]您想要按照您想要设置的顺序附加 的值。在这个答案中,我想提出以下示例脚本?此示例脚本使用 googleapis for python。因此,请执行python 快速入门流程。此脚本使用service = build('sheets', 'v4', credentials=creds)从此快速入门中检索的内容。
service = build('sheets', 'v4', credentials=creds) # This is from the above Quickstart.
order = ['name', 'post_code', 'address1', 'phone'] # This is the order you want to put the values.
businesses = [{'name': 'Coffee_Shop', 'post_code': 'E6 7HJ', 'address1': '87 Longbridge Road', 'phone': '0773827354'}] # This is from your question.
spreadsheet_id = '###' # Please set the Spreadsheet ID.
sheet_name = 'Sheet1' # Please set the sheet name.
values = [[o[e] for e in order] for o in businesses]
res = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=sheet_name, valueInputOption='USER_ENTERED', body={'values': values}).execute()
Run Code Online (Sandbox Code Playgroud)
['name', 'post_code', 'address1', 'phone']。如果您想使用 python 的 gspread 来实现此目的,您还可以使用以下脚本。
client = gspread.authorize(credentials) # Please use this line for your script.
spreadsheet_id = '###' # Please set the Spreadsheet ID.
sheet_name = 'Sheet1' # Please set the sheet name.
order = ['name', 'post_code', 'address1', 'phone'] # This is the order you want to put the values.
businesses = [{'name': 'Coffee_Shop', 'post_code': 'E6 7HJ', 'address1': '87 Longbridge Road', 'phone': '0773827354'}] # This is from your question.
values = [[o[e] for e in order] for o in businesses]
spreadsheet = client.open_by_key(spreadsheetId)
sheet = spreadsheet.worksheet(sheetName)
sheet.append_rows(values, value_input_option='USER_ENTERED')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1218 次 |
| 最近记录: |