我正在做一个为期100天的python代码挑战,这个挑战让我受益匪浅。
我用的是薄片酒和龙舌兰酒。代码应该查看我谷歌表格中的“城市”列,获取每个城市的IATA代码,然后用代码更新表格。Print()显示程序正在读取工作表中的信息,但IATA代码列仍为空。正在读取表中的数据,但我无法获取iata代码并将其放入表中。
我试过:
查看Sheety文档,并将键、值对更改为所需的值。
确保Sheety设置为启用get、put和post。
检查我的缩进
检查拼写错误
检查我是否更改了变量的名称等,因为我在几天内这样做了。
插入print命令以查看json正在做什么。
import os
import requests
#CONSTANT
SHEETY_API = os.getenv("SHEETY_API")
SHEETY_ENDPOINT = "https://api.sheety.co/d741b494d378a4eff912af14a92fd22e/metroSkies/prices/2"
class DataManager:
# Initialization
def __init__(self):
self.destination_data = {}
def get_destination_data(self):
headers = {"Authorization": f"Bearer {SHEETY_API}"}
response = requests.get(url=SHEETY_ENDPOINT, headers=headers)
response.raise_for_status()
data = response.json()
print(response.json)
self.destination_data = data["prices"]
return self.destination_data
def update_destination_codes(self, updated_data):
for destination in updated_data:
new_data = {
"price": {
"iataCode": destination["city"]
}
}
response = requests.put(
url=f"{SHEETY_ENDPOINT}/prices/{destination['id']}",
json=new_data,
headers={"Authorization": f"Bearer {SHEETY_API}"}
)
print(response.text) # For debugging