【问题标题】:obtaining dictionary representation for a JSON, saved as string in file获取 JSON 的字典表示,保存为文件中的字符串
【发布时间】:2014-03-27 08:14:24
【问题描述】:

我正在使用 twitter 数据集,我已将提取的一些推文转换为特定文件,同时我使用了fil.write(str(tweet)+"\n"),其中推文是由tweet = json.loads(item) 获得的正确字典。现在,当我逐行打开提取的文件时

amr = open('tweets-1385844523.json')

akw = open("misw","w")


for line in amr:
    flag =0
    print type(line)
    twe = json.loads(line)

但在这里我收到以下错误

Traceback (most recent call last):
  File "check2.py", line 17, in <module>
    twe = json.loads(line)
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 381, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 1 (char 1)

文件样本是

{u'contributors': None, u'truncated': False, u'text': u'How the heck am I supposed to walk with these on? \U0001f631 http://t.co/ndQlFY6ZaD', u'in_reply_to_status_id': None, u'id': 406887535240298496, u'favorite_count': 0, u'source': u'<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>', u'retweeted': False, u'coordinates': None, u'entities': {u'symbols': [], u'user_mentions': [], u'hashtags': [], u'urls': [], u'media': [{u'expanded_url': u'http://twitter.com/CamilaZaggy/status/406887535240298496/photo/1', u'display_url': u'pic.twitter.com/ndQlFY6ZaD', u'url': u'http://t.co/ndQlFY6ZaD', u'media_url_https': u'https://pbs.twimg.com/media/BaWODrCIgAAVUzW.jpg', u'id_str': u'406887535089319936', u'sizes': {u'small': {u'h': 455, u'resize': u'fit', u'w': 340}, u'large': {u'h': 1024, u'resize': u'fit', u'w': 764}, u'medium': {u'h': 804, u'resize': u'fit', u'w': 599}, u'thumb': {u'h': 150, u'resize': u'crop', u'w': 150}}, u'indices': [52, 74], u'type': u'photo', u'id': 406887535089319936, u'media_url': u'http://pbs.twimg.com/media/BaWODrCIgAAVUzW.jpg'}]}, u'in_reply_to_screen_name': None, u'id_str': u'406887535240298496', u'retweet_count': 0, 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': 1036100580, u'verified': False, u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/378800000661722703/a22f21ef022be63e2f22f64002065e11_normal.jpeg', u'profile_sidebar_fill_color': u'DDEEF6', u'profile_text_color': u'333333', u'followers_count': 70, u'profile_sidebar_border_color': u'FFFFFF', u'id_str': u'1036100580', u'profile_background_color': u'FFFFFF', u'listed_count': 0, u'profile_background_image_url_https': u'https://si0.twimg.com/profile_background_images/872742115/e93bd4da46567ab1d785b8de7e4fe16a.jpeg', u'utc_offset': -14400, u'statuses_count': 1024, u'description': u"I'm the happiest when I'm in concerts \u270c", u'friends_count': 264, u'location': u'IG: heyheymila', u'profile_link_color': u'F5A6F5', u'profile_image_url': u'http://pbs.twimg.com/profile_images/378800000661722703/a22f21ef022be63e2f22f64002065e11_normal.jpeg', u'following': None, u'geo_enabled': True, u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/1036100580/1385170913', u'profile_background_image_url': u'http://a0.twimg.com/profile_background_images/872742115/e93bd4da46567ab1d785b8de7e4fe16a.jpeg', u'name': u'\u2744 Camila \u2744', u'lang': u'en', u'profile_background_tile': False, u'favourites_count': 182, u'screen_name': u'CamilaZaggy', u'notifications': None, u'url': None, u'created_at': u'Wed Dec 26 02:31:29 +0000 2012', u'contributors_enabled': False, u'time_zone': u'Atlantic Time (Canada)', 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'en', u'created_at': u'Sat Nov 30 20:48:42 +0000 2013', u'filter_level': u'medium', u'in_reply_to_status_id_str': None, u'place': None}

【问题讨论】:

  • 你为什么不试试json.loads(open('...json').read())
  • @thefourtheye 我需要一个一个打开而不是整个文件,因为数据很大。

标签: python json python-2.7 twitter


【解决方案1】:

您正在按行切割它,这不是解析 json 的方式。事实上——这几乎是不可能的。标准的json 库为此提供了一个功能。使用json.load(amr)

【讨论】:

    【解决方案2】:

    您正在使用str(tweet) 对日期进行编码,然后尝试使用json.load()(或json.joads())对其进行解码。两人简直不搭。他们使用不同的规则,他们假设不同的代码。

    例如,str({ "foo": "bar" }) 将导致 {'foo': 'bar'}(注意单引号),而 Json 可以解析像 {"foo": "bar"} 这样的字符串(带有双引号)。

    最好的解决方案当然是使用 Json 代码进行编码,即。 e.使用

    fil.write(json.dumps(tweet)+"\n")
    

    而不是在创建时。

    如果这不再可能(听起来有点像您已经拥有一个大型数据库),您可以使用

    twe = eval(line)
    

    解析旧数据。那时你根本不会使用 Json ;-)

    但请注意,使用eval 可能存在安全问题,以防其输入不是您自己的数据而是由可能的入侵者生成的。

    【讨论】:

    • 是否有其他出路,重新运行创建代码需要数周时间
    • 我添加了一个关于这方面的部分:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    相关资源
    最近更新 更多