首页 > 解决方案 > 类型错误:JSON 对象必须是 str,而不是 Jupyter 笔记本上的“字节”

问题描述

完全相同的代码

A=b'["c1006","c1007"]'
json.loads(A)

在 Python 控制台中工作,但在 Jupyter 笔记本中不起作用,说

TypeError: the JSON object must be str, not 'bytes'

为什么以及如何修复/编写可移植的?


我在 Linux 上使用 Python 3.x:

(py36) dims@calculon:~$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> A= b'["c1006","c1007"]'
>>> json.loads(A)
['c1006', 'c1007']

标签: pythonjsonutf-8jupyter

解决方案


为我工作:

The version of the notebook server is: 5.6.0
The server is running on this version of Python:
Python 3.6.6 |Anaconda custom (64-bit)| (default, Jun 28 2018, 11:27:44) [MSC v.1900 64 bit (AMD64)]

A=b'["c1006","c1007"]'
json.loads(A)

['c1006', 'c1007']

检查您的版本,似乎运行的 python 版本与您的 anaconda 不同:

import sys 
sys.version

推荐阅读