推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
PbCopy111
V2EX  ›  Python

Python 学习问题, json 格式不对

  •  
  •   PbCopy111 · Oct 14, 2019 · 3241 views
    This topic created in 2420 days ago, the information mentioned may be changed or developed.

    学习 python,用的一个源代码:

    https://github.com/rollingstarky/Python-Voice-Assistant/blob/master/src/ai.py

    就是录音+百度识别+图灵

    这个代码有点老了,我这里运行的错误提示如下:

    please say something
    you said: 你好
    Traceback (most recent call last):
      File "aitalk.py", line 129, in <module>
        response = robot(request)
      File "aitalk.py", line 78, in robot
        response_dict = json.loads(response.text)
      File "/Users/v2ex/.pyenv/versions/3.6.0/lib/python3.6/json/__init__.py", line 354, in loads
        return _default_decoder.decode(s)
      File "/Users/v2ex/.pyenv/versions/3.6.0/lib/python3.6/json/decoder.py", line 339, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/Users/v2ex.pyenv/versions/3.6.0/lib/python3.6/json/decoder.py", line 357, in raw_decode
        raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
    

    请教应该怎么修改啊??

    7 replies    2019-10-17 13:02:02 +08:00
    guyeu
        1
    guyeu  
       Oct 14, 2019
    你把 response 打印出来不就能知道 json 哪里不对了。。
    ysc3839
        2
    ysc3839  
       Oct 15, 2019 via Android
    这个代码怎么这么奇怪? requests 有 .post() 的方法,还用 request('POST', ...)。requests 有 res.json() 的方法,还用 json.loads()。
    luckyc
        3
    luckyc  
       Oct 15, 2019
    @ysc3839 水平不行呗.
    请求 api 完全不判断一下能否返回结果啊.
    我 request 之后都会 try 一下.


    result = response_dict["results"][0]["values"]["text"]
    还有这个,万一 dict 没有 results 怎么办?
    PbCopy111
        4
    PbCopy111  
    OP
       Oct 15, 2019
    @rollingstarky 看看作者在不在这里。。。。。
    craiiz
        5
    craiiz  
       Oct 15, 2019
    一般情况是第 78 行的时候发生了错误,导致返回的内容已经不是 json,变成了 html 或其他的数据
    cominghome
        6
    cominghome  
       Oct 17, 2019
    我在想你的学习计划是不是安排得有问题,拧螺丝都没拧熟就准备开始造火箭啦?
    PbCopy111
        7
    PbCopy111  
    OP
       Oct 17, 2019
    @cominghome 没办法,都说看了基础,开始做程序,我就看看别人的源码,然后慢慢来呗。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2624 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 15:39 · PVG 23:39 · LAX 08:39 · JFK 11:39
    ♥ Do have faith in what you're doing.