大佬教程收集整理的这篇文章主要介绍了Python~JSON 错误 → json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的 discord bot 上工作并找到了一个我想使用的 API(名为 numapi),我在我的电脑上做了一个原型↓↓↓↓ 并且它工作正常。
import requests
import Json
def numAPI(no):
response = requests.get(f'http://numbersAPI.com/{no}?Json')
#print(response.Json())
Json_data = Json.loads(response.text)
num = Json_data['text']
return(num)
while True:
inc = input('enter ')
@H_675_6@
但是当我在我的不和谐机器人中复制同样的东西时,它就不起作用了。代码和错误如下:-
代码
import requests
import Json
clIEnt = discord.ClIEnt()
def numAPI(no):
#http://numbersAPI.com/#505/year
response = requests.get(f'http://numbersAPI.com/{no}?Json')
#print(response.Json())
Json_data = Json.loads(response.text)
num = Json_data['text']
return(num)
@clIEnt.event
async def on_message(messagE):
if message.content.startswith('$fact'):
number = message.content.split('$fact',1)[1]
fact = numAPI(number)
#await message.chAnnel.send(number)
await message.chAnnel.send(fact)
clIEnt.run(os.getenv("TOKEN"))
@H_675_6@
错误
Ignoring exception in on_message
TraceBACk (most recent call last):
file "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/clIEnt.py",line 343,in _run_event
await coro(*args,**kwargs)
file "main.py",line 291,in on_message
fact = numAPI(number)
file "main.py",line 89,in numAPI
Json_data = Json.loads(response.text)
file "/usr/lib/python3.8/Json/__init__.py",line 357,in loads
return _default_decoder.decode(s)
file "/usr/lib/python3.8/Json/decoder.py",line 337,in decode
obj,end = self.raw_decode(s,IDx=_w(s,0).end())
file "/usr/lib/python3.8/Json/decoder.py",line 355,in raw_decode
raise JsONDecodeError("ExpecTing value",s,err.value) from None
Json.decoder.JsONDecodeError: ExpecTing value: line 1 column 1 (char 0)
@H_675_6@
请帮我解决这个问题
json.loads()@H_675_6@ 假定数据已编码。可能您正在接收一个字节字符串。试试
response.text.decode('utf-8')@H_675_6@。
以上是大佬教程为你收集整理的Python~JSON 错误 → json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)全部内容,希望文章能够帮你解决Python~JSON 错误 → json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。