【问题标题】:ValueError: Expecting property name: line 1 column 2 (char 1)ValueError:期望属性名称:第 1 行第 2 列(字符 1)
【发布时间】:2014-12-02 08:18:43
【问题描述】:

我有一个数据库,其中包含我使用 Twitter API 抓取的大量推文。这些推文采用 json 格式,我可以将它们转换成字典,对吧?所以我们开始吧:我正在尝试使用 json 包将这些字符串转换为字典。但是每次我尝试运行 json.loads(string) 时,它都会给我一个错误:

ValueError:期望属性名称:第 1 行第 2 列(字符 1)。

这是我的数据库中的“json 字符串”示例。

{u"contributors": "None", u"truncated": False, u"text": u"RT @WhoScored: Most clear-cut chances missed at World Cup 2014: M\xfcller / Higua\xedn / Benzema (5), de Vrij / \xd6zil / Ronaldo (4)", u"in_reply_to_status_id": "None", u"id": 487968527395983360, u"favorite_count": 0, u"source": u"<a href="http://blackberry.com/twitter" rel="nofollow">Twitter for BlackBerry\xae</a>", u"retweeted": False, u"coordinates": "None", u"entities": {u"user_mentions": [{u"id": 99806132, u"indices": [3, 13], u"id_str": u"99806132", u"screen_name": u"WhoScored", u"name": u"WhoScored.com"}], u"symbols": [], u"trends": [], u"hashtags": [], u"urls": []}, u"in_reply_to_screen_name": "None", u"id_str": u"487968527395983360", u"retweet_count": 0, u"in_reply_to_user_id": "None", u"favorited": False, u"retweeted_status": {u"contributors": "None", u"truncated": False, u"text": u"Most clear-cut chances missed at World Cup 2014: M\xfcller / Higua\xedn / Benzema (5), de Vrij / \xd6zil / Ronaldo (4)", u"in_reply_to_status_id": "None", u"id": 487955847025143808, u"favorite_count": 17, u"source": u"<a href="https://about.twitter.com/products/tweetdeck" rel="nofollow">TweetDeck</a>", u"retweeted": False, u"coordinates": "None", u"entities": {u"user_mentions": [], u"symbols": [], u"trends": [], u"hashtags": [], u"urls": []}, u"in_reply_to_screen_name": "None", u"id_str": u"487955847025143808", u"retweet_count": 59, u"in_reply_to_user_id": "None", u"favorited": False, u"user": {u"follow_request_sent": "None", u"profile_use_background_image": True, u"default_profile_image": False, u"id": 99806132, u"verified": True, u"profile_image_url_https": u"https://pbs.twimg.com/profile_images/477005408557486083/9MVR7GdF_normal.jpeg", u"profile_sidebar_fill_color": u"DDEEF6", u"profile_text_color": u"333333", u"followers_count": 425860, u"profile_sidebar_border_color": u"C0DEED", u"id_str": u"99806132", u"profile_background_color": u"272727", u"listed_count": 3245, u"profile_background_image_url_https": u"https://pbs.twimg.com/profile_background_images/439356280/123abc.jpg", u"utc_offset": 3600, u"statuses_count": 24118, u"description": u"The largest detailed football statistics website, covering Europe"s top leagues and more. Follow @WSTipster for betting tips. Powered by Opta data.", u"friends_count": 67, u"location": u"London", u"profile_link_color": u"0084B4", u"profile_image_url": u"http://pbs.twimg.com/profile_images/477005408557486083/9MVR7GdF_normal.jpeg", u"following": "None", u"geo_enabled": False, u"profile_banner_url": u"https://pbs.twimg.com/profile_banners/99806132/1402565693", u"profile_background_image_url": u"http://pbs.twimg.com/profile_background_images/439356280/123abc.jpg", u"name": u"WhoScored.com", u"lang": u"en", u"profile_background_tile": False, u"favourites_count": 250, u"screen_name": u"WhoScored", u"notifications": "None", u"url": u"http://whoscored.com", u"created_at": u"Sun Dec 27 23:22:45 +0000 2009", u"contributors_enabled": False, u"time_zone": u"London", u"protected": False, u"default_profile": False, u"is_translator": False}, u"geo": "None", u"in_reply_to_user_id_str": "None", u"possibly_sensitive": False, u"lang": u"ro", u"created_at": u"Sat Jul 12 13:45:14 +0000 2014", u"filter_level": u"low", u"in_reply_to_status_id_str": "None", u"place": "None"}, u"user": {u"follow_request_sent": "None", u"profile_use_background_image": True, u"default_profile_image": False, u"id": 498676612, u"verified": False, u"profile_image_url_https": u"https://pbs.twimg.com/profile_images/485720258934603776/BmUaZHax_normal.jpeg", u"profile_sidebar_fill_color": u"DDEEF6", u"profile_text_color": u"333333", u"followers_count": 192, u"profile_sidebar_border_color": u"C0DEED", u"id_str": u"498676612", u"profile_background_color": u"C0DEED", u"listed_count": 1, u"profile_background_image_url_https": u"https://pbs.twimg.com/profile_background_images/654833637/l9fp65m0xqzsmoneg8pz.jpeg", u"utc_offset": "None", u"statuses_count": 6468, u"description": u"Garut 10 July | Asda islamic school | Farmasi | @judikajude  | path : Dicky Darul Majid", u"friends_count": 153, u"location": u"", u"profile_link_color": u"B30000", u"profile_image_url": u"http://pbs.twimg.com/profile_images/485720258934603776/BmUaZHax_normal.jpeg", u"following": "None", u"geo_enabled": True, u"profile_banner_url": u"https://pbs.twimg.com/profile_banners/498676612/1404927261", u"profile_background_image_url": u"http://pbs.twimg.com/profile_background_images/654833637/l9fp65m0xqzsmoneg8pz.jpeg", u"name": u"DICKY ", u"lang": u"en", u"profile_background_tile": False, u"favourites_count": 74, u"screen_name": u"DickyDarulMajid", u"notifications": "None", u"url": "None", u"created_at": u"Tue Feb 21 09:21:59 +0000 2012", u"contributors_enabled": False, u"time_zone": "None", u"protected": False, u"default_profile": False, u"is_translator": False}, u"geo": "None", u"in_reply_to_user_id_str": "None", u"possibly_sensitive": False, u"lang": u"ro", u"created_at": u"Sat Jul 12 14:35:37 +0000 2014", u"filter_level": u"medium", u"in_reply_to_status_id_str": "None", u"place": "None"}

这里是代码:

import sys, codecs, json
encode = sys.stdin.encoding

all_entries = Tweet.objects.all()[1427:1435]

for entry in all_entries:
    tweet = entry.tweet.encode(encode)
    json_acceptable_string = tweet.replace("'", "\"")
    json_acceptable_string = json_acceptable_string.replace("None", "\"None\"")
    data = json.loads(json_acceptable_string)
    print data

追溯:

Traceback (most recent call last):
File "/home/kiko/workspace/SA_WorldCup/main.py", line 6, in <module>
util.tweets_count()
File "/home/kiko/workspace/SA_WorldCup/util/__init__.py", line 25, in tweets_count
data = json.loads(json_acceptable_string, object_hook=JSONObject)
File "/usr/lib/python2.7/json/__init__.py", line 351, in loads
return cls(encoding=encoding, **kw).decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)

我尝试了很多次,但都没有成功。你能帮我吗?非常感谢。

【问题讨论】:

    标签: python json django python-2.7 twitter


    【解决方案1】:

    看起来你的“json”实际上是 python 文字语法。在这种情况下,ast.literal_eval 字符串1 可能更容易。

    作为附带好处,您不必将None 替换为"None"。 (考虑一条推文,上面写着"None of your base are belong to us"

    1尽管首先确保将正确的 json 转储到数据库中可能会更好...

    【讨论】:

      【解决方案2】:

      使用 eval()

      In [13]: a = "[{'start_city': '1', 'end_city': 'aaa', 'number': 1},\
      ...:      {'start_city': '2', 'end_city': 'bbb', 'number': 1},\
      ...:      {'start_city': '3', 'end_city': 'ccc', 'number': 1}]"
      In [14]: eval(a)
      Out[14]:
      [{'end_city': 'aaa', 'number': 1, 'start_city': '1'},
       {'end_city': 'bbb', 'number': 1, 'start_city': '2'},
       {'end_city': 'ccc', 'number': 1, 'start_city': '3'}
      

      【讨论】:

        猜你喜欢
        • 2014-10-31
        • 2018-06-28
        • 1970-01-01
        • 1970-01-01
        • 2019-05-27
        • 1970-01-01
        • 2018-10-30
        • 2020-02-03
        • 1970-01-01
        相关资源
        最近更新 更多