json读取数据:ValueError: Extra data: line 77 column 2
来源:互联网 发布:php log打印到日志 编辑:程序博客网 时间:2024/05/19 06:50
如果你要读取一个多行的json文件,比如
{"pid": 150400, "id": 150402, "name": "电影票"}{"pid": 150000, "id": 150500, "name": "票务"}{"pid": 150500, "id": 150501, "name": "国内旅游"}{"pid": 150500, "id": 150502, "name": "海外旅游"}
如果你直接使用:
file = open('test.json','r')res = file.read()dic = json.loads(res)
则会抛出异常:ValueError: Extra data: line 2 column 2 - line 4 column 2
表示数据错误,数据太多:第二行-第四行
因为json只能读取一个文档对象,有两个解决办法
1、单行读取文件
2、保存数据源的时候,格式写为一个对象
代码:
方法一.单行读取文件
file = open('test.json','r')for line in file.readlines(): dic = json.loads(line)
方法二.保存数据源的时候,格式写为一个对象
{"cates":[{"pid": 150400, "id": 150402, "name": "电影票"},{"pid": 150000, "id": 150500, "name": "票务"},{"pid": 150500, "id": 150501, "name": "国内旅游"},{"pid": 150500, "id": 150502, "name": "海外旅游"}]}
然后 就是作为一个文档对象处理
file = open('test.json','r')res = file.read()dic = json.loads(res)
参考文章:
作者:coreki
链接:http://www.jianshu.com/p/b6a02b49845c
阅读全文
0 0
- json读取数据:ValueError: Extra data: line 77 column 2
- python解析json报错ValueError: Expecting property name: line 1 column 2 (char 1)
- python JSON ValueError: Expecting property name: line 1 column 2 (char 1)
- simplejson.scanner.JSONDecodeError: Extra data: line 1 column 22089 - line 1 column 22090
- [解决办法]Python中使用json.loads解码字符串时出错:ValueError: Expecting property name: line 1 column 2 (char 1)
- Python解析json之ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
- [解决办法]Python中使用json.loads解码字符串时出错:ValueError: Expecting property name: line 1 column 2 (char 1)
- [解决办法]Python中使用json.loads解码字符串时出错:ValueError: Expecting property name: line 1 column 2 (char 1)
- Python中使用json.loads解码字符串时出错:ValueError: Expecting property name: line 1 column 1 (char 1)
- GP通过外部表装载数据时遇到ERROR:extra data after last expected column解决方法
- firefox报错 关于 JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
- 关于SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data错误的解决
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data情况在easyui 的datagrid
- 集成支付宝时:JSON.parse: unexpected character at line 1 column 1 of the JSON data
- 集成支付宝时:JSON.parse: unexpected character at line 1 column 1 of the JSON data
- SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON火狐
- python 字符串转换为json时,出现Extra data错误
- 读取MYSQL中文数据乱码,解决data too long for column 'title' at row 1
- Linux查看物理CPU个数、核数、逻辑CPU个数
- sandbox
- Ubuntu下安装Opencv2.4.9 及实现python接口
- android打造酷炫自定义ProgressBar
- 【Java并发之】BlockingQueue
- json读取数据:ValueError: Extra data: line 77 column 2
- 网络编程中的常用函数整理
- 怎么搞积分
- Unsupported major.minor version 51.0 JDK版本错误
- JS按时间戳去分类
- XML文件报错dubbo:XX解决方法
- 关于香橙派H3的一些问题
- 团队项目选题-移动端模拟经营类游戏
- 实用js代码