ValueError:控制字符无效:第115076行第173行

时间:2016-07-13 02:21:08

标签: python json

我在python中解析json文件时遇到问题。

我的代码在这里:

import json
from pprint import pprint

with open('review_sample.json') as data_file:
data = json.load(data_file)
pprint(data)

json文件格式在这里:

    {
    "table": "TempTable",
    "rows":
    [
        {
            "comment_id": "R1KLDHE77IOLUM",
            "crawl_time": "2015-07-17 22:55:16",
            "title": "Excellent TV, excellent price... but look out for bugs.",
            "overall_rating": "5",
            "purchase": "Verified Purchase",
            "comment": "This is an excellent TV at an excellent price. For those who say that you can't tell the difference between 4k and ***p, I disagree. I compared this side by side to my *** LG 55' ***p set, and the resolution and sharpness of the image is just no comparison. Can you see an individual pixel from a normal viewing distance on either set? Of course not. But you can see when things start to get fuzzy and pixelated with a large ***p set, and that simply is not an issue with 4K. Picture quality is outstanding but you will want to tweak picture settings - I find that 'Standard' and 'Photo' modes are the best right out of the box, but worth customizing. I also turned off TruMotion, which seemed to be creating some lag when gaming, and is also a bit unsettling for movies and TV (which are usually filmed in 24 and 30 FPS, respectively, rather than 120 FPS TruMotion). 4K playback from Netflix and Amazon Instant Video are superb, as is upscaling from a ***p source. I was surprised how great Battlefield Hardline looked when upscaled to 4K. Overall, WebOS 2.0 is a joy to use, though I'm not a huge fan of the Smart Remote - just clunky to use and not really necessary. I had a bit of a scare when suddenly every 20th vertical row of pixels started bugging out in rainbow colors - see photos. I cycled the power and everything was fine, so I suspect that this was a software bug in the upscaling process (was playing Xbox One at ***p at the time). Will update this review if it happens again. Build quality feels good and the TV looks great - very sleek, slim, and minimal bezels.",
            "site": "amazon",
            "brand": "lg",
            "country_code": "us",
            "product_group_name": "tv",
            "product_name": "smarttv",
            "model_name": "4k",
            "model_code": "*UF7600"
        }
    ]
}

如果我有一些评论,它并没有解决问题。但如果我加载完整的json文件(很多评论),则会出现值错误。 错误消息在这里。

Traceback (most recent call last):
  File "D:/kaggle/word2vec/server.py", line 11, in <module>
    data = json.load(data_file)
  File "C:\Anaconda2\lib\json\__init__.py", line 291, in load
    **kw)
  File "C:\Anaconda2\lib\json\__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "C:\Anaconda2\lib\json\decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Anaconda2\lib\json\decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 115076 column 173 (char 8301811)

Process finished with exit code 1

请帮忙。

1 个答案:

答案 0 :(得分:0)

你不能json.loads文件句柄,你应该加载字符串。

我的内容在a.txt

[root @ ebs-49393 tmp] #python test.py

{u&#39; table&#39;:你&#39; TempTable&#39;,你&#39;行&#39;:[{u&#39;评论&#39;:你&#34;这是一个很好的电视以优惠的价格。对于那些说你无法区分4k和 * p的人,我不同意。我把这个并排比作我的* LG 55&#39; *** p设置,图像的分辨率和清晰度无法比较。你能看到任何一组中正常观看距离的单个像素吗?当然不是。但你可以看到事情开始变得模糊并且用大的*** p设置像素化,这对4K来说根本就不是问题。图片质量非常出色,但您需要调整图片设置 - 我发现&#39;标准版&#39;和照片&#39;模式是最好的开箱即用,但值得定制。我也关闭了TruMotion,这似乎在游戏时产生了一些滞后,对电影和电视也有点不稳定(通常分别以24和30 FPS拍摄,而不是120 FPS TruMotion)。来自Netflix和亚马逊即时视频的4K播放效果非常出色,而且从***来源升级也是如此。令我感到惊讶的是,当升级到4K时,战地硬线看起来很棒。总的来说,WebOS 2.0是一种使用的乐趣,虽然我不是智能遥控器的忠实粉丝 - 只是笨拙地使用而不是非常必要。当突然每隔20行像素开始用彩虹色调时,我有点害怕 - 见照片。我循环了电源,一切都很好,所以我怀疑这是升级过程中的一个软件错误(当时在*** p播放Xbox One)。如果再次发生,将更新此评论。构建质量感觉很好,电视看起来很棒 - 非常时尚,纤薄,最小的边框。&#34;,购买&#39;:u&#39;已验证购买&#39;,&#39; model_code&#39 ;:你&#39; * UF7600&#39;,你&#39;标题&#39;:优秀的电视,优惠的价格......但要注意缺陷。&#39;,u&#39;品牌&# 39;:你&#39;,你&#39;评论_id&#39;:你&#39; R1KLDHE77IOLUM&#39;,你&#39;网站&#39;:你&#39;亚马逊&#39;,你&# 39; model_name&#39 ;: u&#39; 4k&#39;,u&#39; crawl_time&#39;:u&#39; 2015-07-17 22:55:16&#39;,u&#39; country_code&#39 ;:你我们&#39;,你&#39; product_group_name&#39;:你&#39; tv&#39;,你&#39; product_name&#39;:你&#39; smarttv&#39;,你&#39; ; overall_rating&#39;:u&#39; 5&#39;}}}

[root @ ebs-49393 tmp] #cat test.py

导入json

buf = open(&#39; ./ a.txt&#39;)。read()

j = json.loads(buf)

print j