python – 将包含多行JSON的文件加载到Pandas中
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 将包含多行JSON的文件加载到Pandas中,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2996字,纯文字阅读大概需要5分钟。
内容图文
![python – 将包含多行JSON的文件加载到Pandas中](/upload/InfoBanner/zyjiaocheng/712/91f94d329f7f4a53aca83bad52d2b983.jpg)
我试图在JSON文件中读入Python pandas(0.14.0)数据帧.这是JSON文件的第一行:
{"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "P_Mk0ygOilLJo4_WEvabAA", "review_id": "OeT5kgUOe3vcN7H6ImVmZQ", "stars": 3, "date": "2005-08-26", "text": "This is a pretty typical cafe. The sandwiches and wraps are good but a little overpriced and the food items are the same. The chicken caesar salad wrap is my favorite here but everything else is pretty much par for the course.", "type": "review", "business_id": "Jp9svt7sRT4zwdbzQ8KQmw"}
我正在尝试执行以下操作:df = pd.read_json(path).
我收到以下错误(完全追溯):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/d/anaconda/lib/python2.7/site-packages/pandas/io/json.py", line 198, in read_json
date_unit).parse()
File "/Users/d/anaconda/lib/python2.7/site-packages/pandas/io/json.py", line 266, in parse
self._parse_no_numpy()
File "/Users/d/anaconda/lib/python2.7/site-packages/pandas/io/json.py", line 483, in _parse_no_numpy
loads(json, precise_float=self.precise_float), dtype=None)
ValueError: Trailing data
什么是尾随数据错误?如何将其读入数据框?
根据一些建议,这里有几行.json文件:
{"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "P_Mk0ygOilLJo4_WEvabAA", "review_id": "OeT5kgUOe3vcN7H6ImVmZQ", "stars": 3, "date": "2005-08-26", "text": "This is a pretty typical cafe. The sandwiches and wraps are good but a little overpriced and the food items are the same. The chicken caesar salad wrap is my favorite here but everything else is pretty much par for the course.", "type": "review", "business_id": "Jp9svt7sRT4zwdbzQ8KQmw"}
{"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "TNJRTBrl0yjtpAACr1Bthg", "review_id": "qq3zF2dDUh3EjMDuKBqhEA", "stars": 3, "date": "2005-11-23", "text": "I agree with other reviewers - this is a pretty typical financial district cafe. However, they have fantastic pies. I ordered three pies for an office event (apple, pumpkin cheesecake, and pecan) - all were delicious, particularly the cheesecake. The sucker weighed in about 4 pounds - no joke.\n\nNo surprises on the cafe side - great pies and cakes from the catering business.", "type": "review", "business_id": "Jp9svt7sRT4zwdbzQ8KQmw"}
{"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "H_mngeK3DmjlOu595zZMsA", "review_id": "i3eQTINJXe3WUmyIpvhE9w", "stars": 3, "date": "2005-11-23", "text": "Decent enough food, but very overpriced. Just a large soup is almost $5. Their specials are $6.50, and with an overpriced soda or juice, it's approaching $10. A bit much for a cafe lunch!", "type": "review", "business_id": "Jp9svt7sRT4zwdbzQ8KQmw"}
我使用的这个.json文件按照规范在每一行中包含一个JSON对象.
我按照建议尝试了jsonlint.com网站,它给出了以下错误:
Parse error on line 14:
...t7sRT4zwdbzQ8KQmw"}{ "votes": {
----------------------^
Expecting 'EOF', '}', ',', ']'
解决方法:
从Pandas版本0.19.0开始,您可以使用lines参数,如下所示:
import pandas as pd
data = pd.read_json('/path/to/file.json', lines=True)
内容总结
以上是互联网集市为您收集整理的python – 将包含多行JSON的文件加载到Pandas中全部内容,希望文章能够帮你解决python – 将包含多行JSON的文件加载到Pandas中所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。