【问题标题】:json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) and 204 Responsejson.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)和 204 响应
【发布时间】:2020-11-14 04:58:19
【问题描述】:

我一直在尝试在这里寻找解决此问题的方法,但似乎找不到重复的问题。我有一个天气命令,用户可以按邮政编码或城市搜索。打印 JSON 数据时,城市 URL 工作正常,但是当我输入 location 的邮政编码时,它给了我一个 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 错误。我打印出响应代码,它显示为 204 而不是 200 但是当我删除 if location == int(): 语句和另一个 URL 时,打印邮政编码 JSON 数据没有任何问题。

编辑:将我的代码从 if location == int() 更改为 if location.isdigit() 解决了这个问题。

if message.content.lower().startswith('!weather'):
    x = message.content.split(" ", 1)
    location = x[1]
    if location == int():
        url = 'https://api.weatherbit.io/v2.0/forecast/daily?postal_code={}&days=1&units=I&key=c85e55e66bfa4cb0ad9c21a3a038d59e'.format(location)
    else:
        url = 'https://api.weatherbit.io/v2.0/forecast/daily?city={}&units=I&key=c85e55e66bfa4cb0ad9c21a3a038d59e'.format(location)
    res = requests.get(url)
    information = res.json()
    print(information)

【问题讨论】:

  • 你不应该改变你的问题来实现答案。问题/答案将变得无关紧要,对其他人没有帮助。您应该只编辑问题以提供额外信息或解决问题(如果有)。 stackoverflow.com/help/privileges/edit
  • 谢谢,我将其编辑回原始代码,但添加了一个编辑免责声明来解释我所做的更改。

标签: python-3.x discord.py discord.py-rewrite


【解决方案1】:

这是因为代码中的 if 条件 location == int() 将始终导致 false,因为 int() 将始终为您提供 0,除非预期的 location value is 0。您可以通过交换城市和邮政编码网址来检查。

int() 用于获取给定数字或字符串的整数值。在没有任何值的情况下使用它总是会返回0

【讨论】:

  • 感谢您提供这些信息。我一直在学习,这很有帮助。我将代码更改为if location.isdigit():,现在效果很好。再次感谢。
猜你喜欢
  • 2023-03-25
  • 2020-11-11
  • 2017-05-09
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
相关资源
最近更新 更多