我正在尝试使用一个可以转换日期的API。我从一个包含完整日期的文件中检索数据,使用split和slice分别获得日、月和年。我需要发送每个日期,并将转换返回给用户。
我目前拥有的是:
def convert(day, month, year):
gr_to_hb_url = 'https://www.hebcal.com/converter?cfg=json&gy='+ year+ '&gm='+ month+ '&gd='+ day+'&g2h=1'
with urllib.request.urlopen(gr_to_hb_url) as response:
data = response.read()
obj = json.loads(data)
results = [(result['hd'], result['hm'],result['hy']) for result in obj]
return resultshby, hbm, hbd=convert(prep_day, prep_month, prep_year)
print(hby,hbm,hbd)prep_ day /月/年是我如上所述分别从每一天检索的日、月和年。
错误I get TypeError:字符串索引必须是整数。
感谢任何帮助。谢谢!
发布于 2021-11-22 12:25:27
查看请求的输出:
{"gy":2020,"gm":1,"gd":1,"afterSunset":false,"hy":5780,"hm":"Tevet","hd":4,"hebrew":"ד׳ בְּטֵבֵת תש״פ","events":["Parashat Vayigash"]}我认为您可能需要以下内容:
def convert(day, month, year):
gr_to_hb_url = 'https://www.hebcal.com/converter?cfg=json&gy='+ year+ '&gm='+ month+ '&gd='+ day+'&g2h=1'
with urllib.request.urlopen(gr_to_hb_url) as response:
data = response.read()
obj = json.loads(data)
results = (obj['hd'], obj['hm'],obj['hy'])
return results您所看到的错误的原因是,当您迭代字典类型时,您只得到值。在这种情况下,这将类似于以下内容(尽管在迭代字典时不能保证顺序) 2020,1,1,False,...我想你要遍历的第一个元素是类似"Tevet“的东西。如果result的值是"Tevet“,那么运行"Tevet"["hd"]将导致您看到的错误。
https://stackoverflow.com/questions/70065637
复制相似问题