python json错误:ValueError:期望值:第1行第1列(字符0) [英] python json error: ValueError: Expecting value: line 1 column 1 (char 0)

查看:260
本文介绍了python json错误:ValueError:期望值:第1行第1列(字符0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自韩国,所以您可能不太清楚.

I'm from Korea, so probably you don't understand clearly.

我有一个关于通过python加载json文件的问题.

I have a issue about loading json file via python.

with open("D:/twitter/salathe-us-twitter/20110312/SB_DATA_SB/tweets.txt.2011-03-12_01", encoding='UTF8') as f:
for line in f:
    temp = line.partition('|')
    date.append(temp[0])  # date
    tweets_data.append(temp[2])

这是我的python代码. 我分开一行是因为有一些错误.

Here's my python code. I seperate the line because there're some errors.

温度如下:

('20110302141002236', '|', '{"user":{"following":null,"profile_background_image_url":"http:\\/\\/a3.twimg.com\\/profile_background_images\\/141128439\\/2010-07-01_15.33.10.jpg","favourites_count":1,"verified":false,"time_zone":"Pacific Time (US & Canada)","profile_text_color":"333333","follow_request_sent":null,"profile_sidebar_fill_color":"DDEEF6","id_str":"173736821","profile_background_tile":false,"followers_count":19,"created_at":"Mon Aug 02 06:37:45 +0000 2010","description":"Attend CWU and just tryna do me.","is_translator":false,"show_all_inline_media":false,"geo_enabled":true,"profile_link_color":"0084B4","location":"Tacoma, WA","listed_count":1,"profile_sidebar_border_color":"C0DEED","protected":false,"profile_image_url":"http:\\/\\/a3.twimg.com\\/profile_images\\/1208687030\\/Twitter_normal.jpg","lang":"en","name":"Quintin Brown","contributors_enabled":false,"statuses_count":340,"notifications":null,"profile_use_background_image":true,"screen_name":"QBrown15","id":173736821,"utc_offset":-28800,"friends_count":48,"profile_background_color":"C0DEED","url":"http:\\/\\/www.facebook.com\\/#!\\/profile.php?id=1195837597"},"in_reply_to_screen_name":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"contributors":null,"coordinates":null,"retweeted":false,"text":"\\"RT @barr253 I love fat asses. #honesttweet\\" <<< Naw, that\'s an #ObviousTweet","in_reply_to_user_id_str":null,"retweet_count":0,"in_reply_to_status_id":null,"id_str":"43025281954480130","source":"web","created_at":"Wed Mar 02 19:10:01 +0000 2011","truncated":false,"entities":{"user_mentions":[{"indices":[4,12],"id_str":"204626247","name":"John Barr","screen_name":"barr253","id":204626247}],"urls":[],"hashtags":[{"indices":[31,43],"text":"honesttweet"},{"indices":[73,86],"text":"ObviousTweet"}]},"geo":null,"place":{"bounding_box":{"type":"Polygon","coordinates":[[[-120.597461,46.966947],[-120.518162,46.966947],[-120.518162,47.029281],[-120.597461,47.029281]]]},"place_type":"city","name":"Ellensburg","country":"United States","attributes":{},"id":"c95cdb2a983262e5","full_name":"Ellensburg, WA","country_code":"US","url":"http:\\/\\/api.twitter.com\\/1\\/geo\\/id\\/c95cdb2a983262e5.json"},"favorited":false,"id":43025281954480130}\n')
('\n', '', '')

您可以看到('\ n',``,'').这就是为什么我对它们进行分区.

You can see ('\n', '', ''). That's why I partition them.

所以我试图将temp [2]放在json.loads()的参数中.

So I tried to put temp[2] to parameter of json.loads().

但是它说

C:\Python34\python.exe D:/Twitter_project/TEST.py
Traceback (most recent call last):
File "D:/Twitter_project/TEST.py", line 5, in <module>
a = json.loads(temp[2])
File "C:\Python34\lib\json\__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "C:\Python34\lib\json\decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

我的代码有什么问题? 数据不是json格式吗?

What's the problem of my code? The data isn't json format?

因此,现在,我仅使用try,exception来临时读取数据.

So now, I'm just using try, exception to read the data for temporary.

有效.但是我想知道为什么会发生以及如何解决它.

It worked. But I want to know why it happen and how to fix it.

这是我的尝试,除了代码:

Here's my try except code:

import json
date = []
tweets_data = []
with open("D:/twitter/salathe-us-       twitter/20110302/SB_DATA_SB/tweets.txt.2011-03-02_14-05", encoding='UTF8') as f:
for i,line in enumerate(f):
    try:
        temp = line.partition('|')
        date.append(temp[0])
        tweet = json.loads(temp[2])
        tweets_data.append(tweet)
    except:
        continue

推荐答案

首先,我尝试根据此验证器,您已经拥有.

As first i have tried that you have valid JSON format according this validator and you have.

然后,我尝试编写相同的代码,并且以下代码对我来说工作正常,没有任何异常.

Then i have tried to write same code and the following code works fine for me withnout any exceptions.

import json

temp = ('20110302141002236', '|', '{"user":{"following":null,"profile_background_image_url":"http:\\/\\/a3.twimg.com\\/profile_background_images\\/141128439\\/2010-07-01_15.33.10.jpg","favourites_count":1,"verified":false,"time_zone":"Pacific Time (US & Canada)","profile_text_color":"333333","follow_request_sent":null,"profile_sidebar_fill_color":"DDEEF6","id_str":"173736821","profile_background_tile":false,"followers_count":19,"created_at":"Mon Aug 02 06:37:45 +0000 2010","description":"Attend CWU and just tryna do me.","is_translator":false,"show_all_inline_media":false,"geo_enabled":true,"profile_link_color":"0084B4","location":"Tacoma, WA","listed_count":1,"profile_sidebar_border_color":"C0DEED","protected":false,"profile_image_url":"http:\\/\\/a3.twimg.com\\/profile_images\\/1208687030\\/Twitter_normal.jpg","lang":"en","name":"Quintin Brown","contributors_enabled":false,"statuses_count":340,"notifications":null,"profile_use_background_image":true,"screen_name":"QBrown15","id":173736821,"utc_offset":-28800,"friends_count":48,"profile_background_color":"C0DEED","url":"http:\\/\\/www.facebook.com\\/#!\\/profile.php?id=1195837597"},"in_reply_to_screen_name":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"contributors":null,"coordinates":null,"retweeted":false,"text":"\\"RT @barr253 I love fat asses. #honesttweet\\" &lt;&lt;&lt; Naw, that\'s an #ObviousTweet","in_reply_to_user_id_str":null,"retweet_count":0,"in_reply_to_status_id":null,"id_str":"43025281954480130","source":"web","created_at":"Wed Mar 02 19:10:01 +0000 2011","truncated":false,"entities":{"user_mentions":[{"indices":[4,12],"id_str":"204626247","name":"John Barr","screen_name":"barr253","id":204626247}],"urls":[],"hashtags":[{"indices":[31,43],"text":"honesttweet"},{"indices":[73,86],"text":"ObviousTweet"}]},"geo":null,"place":{"bounding_box":{"type":"Polygon","coordinates":[[[-120.597461,46.966947],[-120.518162,46.966947],[-120.518162,47.029281],[-120.597461,47.029281]]]},"place_type":"city","name":"Ellensburg","country":"United States","attributes":{},"id":"c95cdb2a983262e5","full_name":"Ellensburg, WA","country_code":"US","url":"http:\\/\\/api.twitter.com\\/1\\/geo\\/id\\/c95cdb2a983262e5.json"},"favorited":false,"id":43025281954480130}\n')
('\n', '', '')

a = json.loads(temp[2])

在Python 3.5、3.3上进行了测试

Tested on Python 3.5, 3.3

这篇关于python json错误:ValueError:期望值:第1行第1列(字符0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆