获取 JSON 的字典表示形式,在文件中保存为字符串



我正在使用推特数据集,我已经将提取的一些推文转换为特定文件,在这样做的同时,我使用了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}

您使用str(tweet)对日期进行编码,然后尝试使用json.load()(或json.joads())对其进行解码。 两者就是不合适。 他们使用不同的规则,他们假设不同的代码。

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

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

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

而是在创造上。

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

twe = eval(line)

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

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

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

最新更新