【问题标题】:python json error: ValueError: Expecting value: line 1 column 1 (char 0)python json错误:ValueError:期望值:第1行第1列(char 0)
【发布时间】:2017-03-07 08:11:39
【问题描述】:

我是韩国人,可能你没看清楚。

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

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 代码。 因为有一些错误,所以我将这一行分开。

温度如下:

('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', '', '')。这就是我对它们进行分区的原因。

所以我尝试将 temp[2] 放入 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格式?

所以现在,我只是使用 try, exception 来临时读取数据。

成功了。但我想知道它为什么会发生以及如何解决它。

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

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

【问题讨论】:

    标签: python json


    【解决方案1】:

    首先,我已经尝试过根据 validator 你有有效的 JSON 格式,你有。

    然后我尝试编写相同的代码,以下代码对我来说运行良好,没有任何异常。

    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 上测试

    【讨论】:

    • 不...你只是输入并将json格式放入变量中。
    • 什么?您在json.loads(temp[2]) 上写了您有异常,这意味着 JSON 解析失败,但我使用的数据与您相同,没有异常
    • 哦,我的意思是我已经做了同样的事情。我忘记写了。是的。当您将字符串格式放入 json.load() 时,它可以处理数据。但是当我只是在 python 上阅读它们时,它就不起作用了。
    • “只在 python 上阅读它们”是什么意思?顺便说一句json.load()json.loads() 是两种不同的方法! json.loads() 方法的输出是字典,您可以将其作为标准字典访问。
    • 而“read them on”意味着我通过 open 方法或 readline 函数阅读它们。不像你那样输入字符串,实际上只是在 python 上“读取”。查看我写的临时变量。我将它作为读取文件来处理,但你只是将它设置为元组。我不确定你是否清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 2019-07-04
    • 2020-04-18
    • 2021-09-30
    • 2021-03-25
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多