我们如何将字典中的数据写入 google 工作表中的新行?

How do we write data from a dictionary to a new row in google sheets?

我写了一些代码来收集本地企业名称、邮政编码、地址和 phone 号码。 现在我想按照我希望的顺序将它们写到我的 google spreadsheet 的下一行。 我是 gsheets API 的新手。谁能解释一下我们如何做到这一点?

我有一个字典列表,每个字典都将写入 google sheet 中的下一行。

例如,假设我们有这个列表 为了简单起见,我只使用了一本字典,但在实际代码中,我会有不止一本字典:

businesses = [{name: 'Coffee_Shop', post_code: 'E6 7HJ', 'address1': '87 Longbridge Road', phone: '0773827354'}]

更新:

我在使用 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')

我相信你的目标如下。

  • 您想按照您想要设置的顺序附加 businesses = [{name: 'Coffee_Shop', post_code: 'E6 7HJ', 'address1': '87 Longbridge Road', phone: '0773827354'}] 的值。
  • 您想使用 python 实现此目的。

在这个回答中,我想提出以下示例脚本?此示例脚本将 googleapis 用于 python。所以请this flow of Quickstart for 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()
  • 当此脚本为 运行 时,值将按 ['name', 'post_code', 'address1', 'phone'].
  • 的顺序附加到 sheet

注:

  • 如果你想为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')
    

参考文献: