Python 多次设置同一变量无效[已解决]

Python 多次设置同一变量无效[已解决],python,api,datetime,Python,Api,Datetime,编辑:这已经解决了。谢谢你的时间 最近,我一直在开发一个python程序来返回某个日期的案例数。你们能帮帮我吗?预期的行为是返回输入日期的正确活动案例数,但它不返回任何内容。由于对python比较陌生,我不知道是哪部分代码导致了这个问题。我的代码如下: 将请求作为r导入 导入json 从日期时间导入日期时间 #返回南非第一例病例年份日期的代码 data=json.load(r.get('https://api.covid19api.com/country/south-africa?from=20

编辑:这已经解决了。谢谢你的时间

最近,我一直在开发一个python程序来返回某个日期的案例数。你们能帮帮我吗?预期的行为是返回输入日期的正确活动案例数,但它不返回任何内容。由于对python比较陌生,我不知道是哪部分代码导致了这个问题。我的代码如下:

将请求作为r导入
导入json
从日期时间导入日期时间
#返回南非第一例病例年份日期的代码
data=json.load(r.get('https://api.covid19api.com/country/south-africa?from=2020-03-01T00:00:00Z&to=2020-04-01T00:00:00Z')。文本)
数据=数据[20]
数据=数据[“日期”]
data=datetime.strtime(str(数据),“%Y-%m-%dT%H:%m:%SZ”).timetuple().tmyday
打印(数据)
#此函数用于返回一年中输入日期的案例数。到目前为止,该国的投入毫无作用。2020年第81天,南非出现了第一例病例。因此,此函数至少应重新运行1。
def案例(日期、国家):
##从API加载数据,这应该包括全年。
data=json.load(r.get('https://api.covid19api.com/country/south-africa?from=2020-01-01T00:00:00Z&to=2020-12-01T00:00:00Z')。文本)
#设置一些初始内容。
完成=错误
i=1
案例=0
完成时==False:
数据=数据[i]
数据=数据[“日期”]
#从API获取当前案例的年度日期。
current=datetime.strtime(str(数据),“%Y-%m-%dT%H:%m:%SZ”).timetuple().tmyday
#由于API从最早到最晚订购案例,因此会检查并增加案例变量,直到不再是当天。
如果当前==天:
案例+=1
其他:
完成=正确
i+=1
报税表(个案)
印刷品(案例(81,‘南非’)
#2020-03-21T00:00:00Z
似乎重现此问题的最短代码是:

def cases(day, country):
  data=json.loads(r.get('https://api.covid19api.com/country/south-africa?from=2020-01-01T00:00:00Z&to=2020-12-01T00:00:00Z').text)
  done=False
  i = 1
  cases=0
  while done == False:
    data=data[i]
    data=data['Date'] 
    current = datetime.strptime(str(data),'%Y-%m-%dT%H:%M:%SZ').timetuple().tm_yday
    if current == day:
      cases += 1
    else: 
      done= True
    i+=1
  return (cases)
编辑,似乎正确的代码是:


谢谢你的帮助

重新分配
数据
会阻止此代码工作,因为当您再次启动
while
循环并第二次运行
data=data[i]
时,
data
不再包含从API获得的所有结果,而现在只包含一个字符串,其中包含您处理的最后一天的数据

不要在同一上下文中重新分配变量名

def cases(day, country):
  ##Load the data from the API, this should encompass the whole year.
  data=json.loads(r.get('https://api.covid19api.com/country/south-africa?from=2020-01-01T00:00:00Z&to=2020-12-01T00:00:00Z').text)
  #Set some initial stuff.
  cases = 0
  for item in data:
    date = item['Date']
    #Get day of the year of the current case from the API.
    current = datetime.strptime(str(date),'%Y-%m-%dT%H:%M:%SZ').timetuple().tm_yday
    #Since the API orders cases earliest to latest this checks and increments the cases variable until it is no longer the current day.
    if current == day:
      return item['Confirmed']
  return 0

您能否将标题集中在遇到的特定技术问题上?理想情况下,如果您可以在程序文本中硬编码一些最小的样本数据,这将确保知识库条目对其他人仍然有用,即使API不再可用(或者如果其版本更改,或者修改为需要身份验证等)…类似地,让一些样本数据显示问题本身中API返回值的形状将有助于使问题更加独立,这样就不需要执行API调用并读取其响应来确定(1)如何编写答案,或者(2)一个人想投票表决的一个预先存在的答案是否确实正确。请提供预期答案。显示中间结果与您预期的不同之处。也就是说,阅读代码时的第一条评论是:重新分配
数据
通常是一种代码气味。提取某些内容时,请使用不同的变量名,以便于阅读。因此,比如说,
current_item=data[i];当前日期=当前项['date']
——那么当有人看到
str(当前日期)
时,你的意思比你写
str(数据)
时要清楚得多。我还建议你干脆把
I
全部去掉
对于数据中的当前项:
将为您执行所有迭代。
def cases(day, country):
  ##Load the data from the API, this should encompass the whole year.
  data=json.loads(r.get('https://api.covid19api.com/country/south-africa?from=2020-01-01T00:00:00Z&to=2020-12-01T00:00:00Z').text)
  #Set some initial stuff.
  cases = 0
  for item in data:
    date = item['Date']
    #Get day of the year of the current case from the API.
    current = datetime.strptime(str(date),'%Y-%m-%dT%H:%M:%SZ').timetuple().tm_yday
    #Since the API orders cases earliest to latest this checks and increments the cases variable until it is no longer the current day.
    if current == day:
      return item['Confirmed']
  return 0