2014-07-16 4 views
6

내가 트위터에서이 DICT을 json.load려고 정의되지 않은 : 파이썬 JSON은 : 나가서 설명하자면 NameError : 이름이 '거짓'

{"created_at":"Thu Jul 10 20:02:00 +0000 2014","id":487325888950710272,"id_str":"487325888950710272","text":"\u5f81\u9678\u300c\u5de6\u8155\u306e\u7fa9\u624b\u306f\u30db\u30ed\u3060\u300d","source":"\u003ca href=\"http:\/\/twittbot.net\/\" rel=\"nofollow\"\u003etwittbot.net\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1429838018,"id_str":"1429838018","name":"\u3053\u3093\u306a\uff30\uff30\u306f\u5acc\u3060\u3002","screen_name":"iyada_pp","location":"\u516c\u5b89\u5c40\u306e\u3069\u3053\u304b\u3002","url":null,"description":"\u3010\u3053\u3093\u306aPSYCHO-PASS\u306f\u5acc\u3060\u306a\u3011\u3068\u3044\u3046\u304f\u3060\u3089\u306a\u3044\u5984\u60f3bot\u3067\u3059\u3002\u30ad\u30e3\u30e9\u5d29\u58ca\u304c\u6fc0\u3057\u3044\u306e\u3067\u3054\u6ce8\u610f\u304f\u3060\u3055\u3044\u3002","protected":false,"followers_count":99,"friends_count":98,"listed_count":5,"created_at":"Wed May 15 07:52:33 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":12584,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/3661872276\/ab7201283dac5dc1789bb6dfa9b6abe4_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/3661872276\/ab7201283dac5dc1789bb6dfa9b6abe4_normal.jpeg","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"filter_level":"medium","lang":"ja"} 

내가 그 DICT에 json.load()했다. 아래 오류 메시지가 표시되었습니다.

NameError: name 'false' is not defined 

무슨 일이 일어 났습니까?

+1

* 리터럴 * JSON을 파이썬에 붙여 넣으시겠습니까? 해당 값을 ** 문자열 **로로드하십시오. –

+1

JSON "dict"을 _how_ 표시하십시오. 여기 집에서 아무 문제없이로드됩니다 (파이썬 2 및 3). –

답변

14

문자열을 Python에 직접 붙여 넣으려고합니다. 그러지 마십시오. 을 나타내는 문자열 시퀀스입니다.

JSON 만 으로 보이지만 실제 파이썬 코드는 아닙니다.

여기에 내가 그 안에 어떤 이스케이프를 해석 할 수있는 원시 파이썬 문자열으로 이것을로드 :

>>> import json 
>>> json_string = r'''{"created_at":"Thu Jul 10 20:02:00 +0000 2014","id":487325888950710272,"id_str":"487325888950710272","text":"\u5f81\u9678\u300c\u5de6\u8155\u306e\u7fa9\u624b\u306f\u30db\u30ed\u3060\u300d","source":"\u003ca href=\"http:\/\/twittbot.net\/\" rel=\"nofollow\"\u003etwittbot.net\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1429838018,"id_str":"1429838018","name":"\u3053\u3093\u306a\uff30\uff30\u306f\u5acc\u3060\u3002","screen_name":"iyada_pp","location":"\u516c\u5b89\u5c40\u306e\u3069\u3053\u304b\u3002","url":null,"description":"\u3010\u3053\u3093\u306aPSYCHO-PASS\u306f\u5acc\u3060\u306a\u3011\u3068\u3044\u3046\u304f\u3060\u3089\u306a\u3044\u5984\u60f3bot\u3067\u3059\u3002\u30ad\u30e3\u30e9\u5d29\u58ca\u304c\u6fc0\u3057\u3044\u306e\u3067\u3054\u6ce8\u610f\u304f\u3060\u3055\u3044\u3002","protected":false,"followers_count":99,"friends_count":98,"listed_count":5,"created_at":"Wed May 15 07:52:33 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":12584,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/3661872276\/ab7201283dac5dc1789bb6dfa9b6abe4_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/3661872276\/ab7201283dac5dc1789bb6dfa9b6abe4_normal.jpeg","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"filter_level":"medium","lang":"ja"}''' 
>>> json.loads(json_string) 
{u'contributors': None, u'truncated': False, u'text': u'\u5f81\u9678\u300c\u5de6\u8155\u306e\u7fa9\u624b\u306f\u30db\u30ed\u3060\u300d', u'in_reply_to_status_id': None, u'id': 487325888950710272, u'favorite_count': 0, u'source': u'<a href="http://twittbot.net/" rel="nofollow">twittbot.net</a>', u'retweeted': False, u'coordinates': None, u'entities': {u'symbols': [], u'user_mentions': [], u'hashtags': [], u'urls': []}, u'in_reply_to_screen_name': None, u'id_str': u'487325888950710272', 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': 1429838018, u'profile_background_image_url_https': u'https://abs.twimg.com/images/themes/theme1/bg.png', u'verified': False, u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/3661872276/ab7201283dac5dc1789bb6dfa9b6abe4_normal.jpeg', u'profile_sidebar_fill_color': u'DDEEF6', u'profile_text_color': u'333333', u'followers_count': 99, u'profile_sidebar_border_color': u'C0DEED', u'id_str': u'1429838018', u'profile_background_color': u'C0DEED', u'listed_count': 5, u'is_translation_enabled': False, u'utc_offset': None, u'statuses_count': 12584, u'description': u'\u3010\u3053\u3093\u306aPSYCHO-PASS\u306f\u5acc\u3060\u306a\u3011\u3068\u3044\u3046\u304f\u3060\u3089\u306a\u3044\u5984\u60f3bot\u3067\u3059\u3002\u30ad\u30e3\u30e9\u5d29\u58ca\u304c\u6fc0\u3057\u3044\u306e\u3067\u3054\u6ce8\u610f\u304f\u3060\u3055\u3044\u3002', u'friends_count': 98, u'location': u'\u516c\u5b89\u5c40\u306e\u3069\u3053\u304b\u3002', u'profile_link_color': u'0084B4', u'profile_image_url': u'http://pbs.twimg.com/profile_images/3661872276/ab7201283dac5dc1789bb6dfa9b6abe4_normal.jpeg', u'following': None, u'geo_enabled': False, u'profile_background_image_url': u'http://abs.twimg.com/images/themes/theme1/bg.png', u'name': u'\u3053\u3093\u306a\uff30\uff30\u306f\u5acc\u3060\u3002', u'lang': u'ja', u'profile_background_tile': False, u'favourites_count': 0, u'screen_name': u'iyada_pp', u'notifications': None, u'url': None, u'created_at': u'Wed May 15 07:52:33 +0000 2013', u'contributors_enabled': False, u'time_zone': None, u'protected': False, u'default_profile': True, u'is_translator': False}, u'geo': None, u'in_reply_to_user_id_str': None, u'lang': u'ja', u'created_at': u'Thu Jul 10 20:02:00 +0000 2014', u'filter_level': u'medium', u'in_reply_to_status_id_str': None, u'place': None} 
+0

고마워! 실제로 'output.txt'파일에 덤프 한 많은 트위터 메시지 중 한 줄입니다. 각 메시지는이 텍스트 파일에서 한 행이됩니다. 그 메시지를 json.load하는 방법을 제안 해 주시겠습니까? – Charlo123

+0

@ user3602696 : [JSON 파일을 파이썬으로로드 및 파싱하기] (http://stackoverflow.com/a/12451465)를 참조하십시오. –

1

트위터 스트림 당신에게 문자 유니 코드 문자열을 제공합니다.

type(data) 

예를 들어 JSON 역 직렬화를 사용하기 전에 데이터를 UTF-8로 디코딩해야합니다.

json.loads(data.decode('utf-8')) 

그런 다음 사전을 사용하여 스트림을 처리하십시오. 행운을 빌어 요

-3

대소 문자를 구분합니다.

False != false 
관련 문제