2017-10-25 1 views
-2

누군가이 코드 형식 중 하나가 작동하고 다른 코드가 작동하지 않는 이유를 설명 할 수 있습니까?json 형식의 트윗을 파이썬으로 읽는 데 문제가 있습니다.

내가

import json 
with open("python.json") as data_in_file: 
    print (data_in_file) 

내가 오류가 쓸 때 나는

import json 
with open("python.json") as data_in_file: 

내가

의 오류가 작성하는 경우

MacBook-Air-4:risd_programming BCohen$ python3 reading_multiple_json_files.py 
<_io.TextIOWrapper name='python.json' mode='r' encoding='UTF-8' 

>

나는이

import json 
with open("python.json") as data_in_file: 

    for data_in_file in data_in_file: 
     p = json.loads(data_in_file) 
     print(p["created_at"]) 

모든 벌금과 멋쟁이 작성하는 경우

MacBook-Air-4:risd_programming BCohen$ python3 reading_multiple_json_files.py 
    File "reading_multiple_json_files.py", line 11 

            ^
SyntaxError: unexpected EOF while parsing 

그러나, 그 이유는? 또한 위의 이유에서 for 루프가 동일한 변수, 즉 data_in_file을 data_in_file에 포함해야하는 이유는 무엇입니까? 왜 쓸 수 없습니까?

for x in data_in_file: 

??

는 JSON 파일은 다음과 같습니다 참고 :

{ "created_at": "수요일 9월 27일 0시 2분 37초 0000 2017", "ID"912829797428056065 "ID_STR": "912829797428056065" "텍스트": "RT @S_T_O_P_TERROR : 쉴라 잭슨 리 Kneels # NFL 플레이어를 지원하는 집 바닥에 \ n #"아들의 무릎을 꿇고 전화하는 선수 ","소스 ":"\ u003ca href = \ " "http : //twitter.com/download/android \"rel = \ "nofollow \"\ u003e 안드로이드 용 트위터 \ u003c/a \ u003e ","truncated ": false,"in_reply_to_status_id ": null,"in_reply_to_status_id_str ": null in_reply_to_user_id_str ": null,"in_reply_to_screen_name ": null,"user ": {"id ": 38351186,"id_str ":"38351186 ","name ":"Racy ","screen_name ":"in_reply_to_user_id " "RacySicilian", "location": "Everywhere", "url": "http://gab.ai/RacySicilian", "description": "Constitutio 네이티브 보수 기독교인 # 뒤로 블루 # 프로 라이프 #NRA # 2 수비수 # 군대 #SOT #IST와 이스라엘과 함께 # 이스라엘 # 반 이슬람 # 베이컨 # 인필드 # 블루 핸 드 ","번역가 유형 ":"없음 ","보호 된 ": 거짓,"검증 된 " (followers_count) : 24866, "friends_count": 23127, "listing_count": 203, "favourites_count": 88365, "statuses_count": 106639, "created_at": "Thu May 07 02:55:41 +0000 2009", "utc_offset" : -14400, "time_zone": "동부 표준시 (미국 & 캐나다)", "geo_enabled": false, "lang": "en", "contributors_enabled": false, "is_translator": false, "profile_background_color": "000000 ","profile_background_image_url ":"http://abs.twimg.com/images/themes/theme15/bg.png ","profile_background_image_url_https ":"https://abs.twimg.com/images/themes/theme15/bg .png ","profile_background_tile ": false,"profile_link_color ":"DD2E44 ","profile_sidebar_border_color ":"000000 ","profile_sidebar_fill_color ":"000000 ","profile_text_color ":"000000 ","profile_use_background_image ": false,"profile_image_url ":"http : //pbs.twimg. co.kr/profile_images/780820001284497408/fAJo5QKd_normal.jpg ","profile_image_url_https ":"https://pbs.twimg.com/profile_images/780820001284497408/fAJo5QKd_normal.jpg ","profile_banner_url ":"https://pbs.twimg.com/ "default_profile_image": false, "following": null, "follow_request_sent": null, "notifications": null}, "geo": null, "coordinates": null, "null", "profile_banners/38351186/1480972281" "place": null, "contributors": null, "retweeted_status": { "created_at": "Tue Sep 26 23:33:13 +0000 2017", "id": 912822395026722816, "id_str": "912822395026722816" 텍스트 ":"Rep. 쉴라 잭슨 리 Kneels # NFL Players의 지원으로 하우스 층에 \ n # 'ucode "https://t.co/ozifjD9TXj",display_text_range ":0,140],"소스 "의 아들을 무릎 꿇고 전화하는 선수 : "\ u003ca href = \"https : //studio.twitter.true, "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, com = "rel = \"nofollow \ "\ u003eMedia Studio \ u003c/a \ u003e" , "in_reply_to_screen_name": null, "user": { "id": 2976578589, "id_str": "2976578589", "이름": "STOP TERROR", "screen_name": "S_T_O_P_TERROR", "location" 정보 4 테러리즘 폭력의 자유 민주주의 자유 기소 부패를 막기 위해 & 무장 TERROR 보코 하람 ISIS 하마스 알 카에다 터키이란 쿠르드 ","translator_type ":"none ","protected ": 거짓, "followers_count": 15963, "friends_count": 2619, "listing_count": 501, "favourites_count": 21369, "statuses_count": 96487, "created_at": "Tue Jan 13 15:34 : 130000002015 ","utc_offset ": - 25200,"time_zone ":"태평양 표준시 (미국 & 캐나다) ","geo_enabled ": false,"lang ":"en ","contributors_enabled ": false,"is_translator " : false, "profile_background_color": "BADFCD", "profile_backg round_image_url ":"http://abs.twimg.com/images/themes/theme12/bg.gif ","profile_background_image_url_https ":"https://abs.twimg.com/images/themes/theme12/bg.gif " , "profile_background_tim": false, "profile_link_color": "FF0000", "profile_sidebar_border_color": "F2E195", "profile_sidebar_fill_color": "FFF7CC", "profile_text_color": "0C3E53", "profile_use_background_image": true, "profile_image_url" http://pbs.twimg.com/profile_images/758197775611797504/4hUdxgBZ_normal.jpg","profile_image_url_https":"https://pbs.twimg.com/profile_images/758197775611797504/4hUdxgBZ_normal.jpg","profile_banner_url":"https : //pbs.twimg.com/profile_banners/2976578589/1469602182","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo ": null, "coordinates": null, "place": null, "contributors": null, "is_quote_status": false, "extended_tweet": { "full_text": "Rep. 쉴라 잭슨 리는 # NFL 플레이어를지지하는 집 바닥에서 무릎을 꿇고 있습니다. \ n # '암캐의 아들'을 무릎 꿇고 전화하는 트럼프 선수는 인종 차별입니다. \ "https://t.co/UE04GvqHhP","display_text_range" : [ "text": "Trump", "indices": [73], "엔티티": { "해시 태그": [{ "텍스트": "NFL", "인덱스": [60,64]} [], [], [], [], [], [], [], [], []], [] { "id": 912821680266928128, "id_str": "912821680266928128", "색인": [139,162], "media_url": "http://pbs.twimg.com/media/DKr-w6XUEAErYxB.jpg", "media_url_https" : "https://pbs.twimg.com/media/DKr-w6XUEAErYxB.jpg", "url": "https://t.co/UE04GvqHhP", "display_url": "pic.twitter.com/UE04GvqHhP" , "expanded_url": "https://twitter.com/S_T_O_P_TERROR/status/912822395026722816/video/1", "type": "video", "sizes": { "small": { "w": 270, " h ": 180,"resize ":"fit "},"large ": {"w ": 270,"h ": 180,"resize ":"fit "},"thumb ": {"w ": 150 { "w": 270, "h": 180, "resize": "fit"}}, "video_info": { "aspect_ratio", "h": 150, "resize" ": [3,2],"duration_millis ": 154000,"변형 ": [{ "bitrate": 320000, "content_type": "video/mp4", "url": "https://video.twimg.com/amplify_video/912821680266928128/vid/270x180/RhKfm7xhlPXLnGnZ.mp4"}, { "content_type ":"application/x-mpegURL ","url ":"https://video.twimg.com/amplify_video/912821680266928128/pl/Gpir5dgpzvJLeOcJ.m3u8 "}]}}]},"extended_entities ": {"media " : [id ": 912821680266928128,"id_str ":"912821680266928128 ","색인 ": [139,162],"media_url ":"http://pbs.twimg.com/media/DKr-w6XUEAErYxB.jpg " media_url_https ":"https://pbs.twimg.com/media/DKr-w6XUEAErYxB.jpg ","url ":"https://t.co/UE04GvqHhP ","display_url ":"pic.twitter.com/ UE04GvqHhP ","expanded_url ":"https://twitter.com/S_T_O_P_TERROR/status/912822395026722816/video/1 ","type ":"video ","sizes ": {"small ": {"w ": 270 , "h": 180, "resize": "fit"}, "thumb": { "w": 270 " : 150, "h": 150, "resize": "crop"}, "medium": { "w": 270, "h": 180, "resize": "fit"}}, "video_info" "aspect_ratio": [3,2], "duration_millis": 154000, "variant": [{ "bitrate": 320000, "content_type": "video/mp4", "url": "https : // v ideo.twimg.com/amplify_video/912821680266928128/vid/270x180/RhKfm7xhlPXLnGnZ.mp4"},{content_type":"application/x-mpegURL",url":"https://video.twimg.com/amplify_video/ 0, "reply_count": 2, "retweet_count": 5, "favorite_count": 3, "entities": { "해시 태그": [ { "텍스트": "인덱스": [73,79]}, "URL": {{ "url" : "https://t.co/ozifjD9TXj", "expanded_url": "https://twitter.com/i/web/status/912822395026722816", "display_url": "twitter.[], [기호] : []}, "즐겨 찾기": false, "retweeted": false, 0, "reply_count": 0, "retweet_count": 0, "favorite_count": false, "filter_level": "low", "lang": "en"}, "is_quote_status": false, "quote_count" : 0, "엔터티": { "해시 태그": [{ "텍스트": "NFL", "인덱스": [80,84], { "텍스트": "트럼프", "인덱스": [93,99 [{ "screen_name": "S_T_O_P_TERROR", "이름": "STOP TERROR", "id": 2976578589, "id_str": "2976578589", "urls" ","기호 ": [],"즐겨 찾기 ": false,"retweeted ": false,"filter_level ":"낮음 ","lang ":"en ","timestamp_ms " "id": "912829800338935808", "text": ". @ realDonaldTrump @USUN 최고! \ ud83d \ udc4d @nikkihaley는 훌륭한 선택이었습니다. # #USA Ambassador 2 #UN! \ u003ca href = \ "http : //www.twitter.com \"rel = \ "nofollow \"\ u003e 블랙 베리에 대한 트위터 \ u003c/a \ u003e " "in_reply_to_status_id": "912825663694016522", "in_reply_to_user_id": 25073877, "in_reply_to_user_id_str": "25073877", "in_reply_to_screen_name": "realDonaldTrump", "user": { "id ": 717398291571412992,"id_str ":"717398291571412992 ","name ":"진실 정치 ","screen_name ":"truthinppt ","location ":"영국 ","url ": null,"description " 좌익, 자유 주의자, 스코틀랜드 Nats, EU, PC, MSM, 인종 차별 주의자, 근본 주의자 주의자/무신론자에 의해 거짓말을하는 아프다 & anti #Trump & anti #Brexit 광신도 ","없음 ","보호 된 " : false, "확인 됨": false, "followers_count": 1727, "friends_count": 1695, "listing_count": 16, "favourites_count": 14272, "statuses_count": 3284, "created_at": "Tue Apr 05 17:07 : 36 +0000 2016 ","utc_offset ": null,"time_zone ": 뉴 false, "is_translator": false, "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com", "geo_enabled": false, "lang": "en", "contributors_enabled" /images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","profile_background_tile":false,"profile_link_color":"981CEB ","profile_sidebar_border_color ":"000000 ","profile_sidebar_fill_color ":"000000 ","profile_text_color ":"000000 ","profile_use_background_image ": false,"profile_image_url ":"http://pbs.twimg.com/profile_images/717407296616341505 /_pgy4rYs_normal.jpg","profile_image_url_https":"https://pbs.twimg.com/profile_images/717407296616341505/_pgy4rYs_normal.jpg","profile_banner_url":"https://pbs.twimg.com/profile_banners/717398291571412992/1459879800 ","default_profile ": false,"default_profile_image ": false,"following ": null,"follow_request_sent ": null,"notifications ": null},"geo ": null,"coordinates ": null,"place ": null , "contributors": null, "is_quote_status": false, "" 0 ","retweet_count ": 0,"favorite_count ": 0,"entities ": {"hashtags ": [{"text ":"미국 ","인덱스 ": [72,76 {97,103}}, { "text": "MAGA", { "text": " "{" "screen_name": "realDonaldTrump", "name": "Donald J. Trump", "id": 25073877, "id_str" ":"25073877 ","indices ": [1,17]}, {"screen_name ":"USUN ","name ":"미국 선교부 유엔 ","id ": 249677516,"id_str ":"249677516 ","indices ": [18,23]}, {"screen_name ":"nikkihaley ","name ":"Nikki Haley ","id ": 37666984,"id_str ":"37666984 ","indices " false ","filter_level ":"low ","lang ":"en ","timestamp_ms ":"1506470558615 ""false ","즐겨 찾기 ": false,"retweeted " } { "created_at": "Wed Sep 27 00:02:38 +0000 2017", "id": 912829801781657606, "id_str": "912829801781657606", "text": "RT @ Unpersuaded112 : Mr. # Trump @realDonaldTrump 당신이 작은 사람들을위한 캠페인에서 당신이 할 것이라고 말한 모든 것은 분명히 거짓말이었습니다. ht \ u2026 ","source ":"\ u003ca href = \ "http : //twitter.com/download/iphone \"rel = \ "nofollow \"\ u003e iPhone 용 트위터 \ u003c/a \ u003e ","truncated 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 ": 784800285059387392,"id_str ":" "null", "description": null, "translator_type": "none", "protected": false로 설정 한 경우 : "784800285059387392", "name": "cookie", "screen_name": "Redheaddiva79" , "확인 됨": false, "followers_count": 79, "friends_count": 259, "listing_count": 2, "favourites_count": 3751, "statuses_count": 4735, "created_at": "Sat Oct 08 16:59:03 null, "time_zone": null, "geo_enabled": false, "lang": "en", "contributors_enabled": false, "is_translator": false, "profile_background_color": "F5F8FA" "profile_background_image_url": "", "profile_background_image_url_https": "", "profile_background_tile": false, "profile_link_color": "1DA1F2", "profile_sidebar_border_color": "C0DEED", "profile_background_image_url" sidebar_fill_color ":"DDEEF6 ","profile_text_color ":"333333 ","profile_use_background_image ": true,"profile_image_url ":"http : //pbs.twimg.com/profile_images/784924483853414400/5GmENSLR_normal.jpg ","profile_image_url_https ":"https://pbs.twimg.com/profile_images/784924483853414400/5GmENSLR_normal.jpg ","default_profile ": true,"default_profile_image ": false,"following " : null, "follow_request_sent": null, "notifications": null, "geo": null, "coordinates": null, "place": null, "contributors": null, "retweeted_status": { "created_at": " Tue Sep 26 19:03:12 +0000 2017 ","id ": 912754444831543298,"id_str ":"912754444831543298 ","text ":"Mr. #Trump @realDonaldTrump 당신이 작은 사람들을 위해 캠페인에서 할 것이라고 말한 모든 것은 분명히 거짓말이었습니다. https://t.co/mVfe6OYwxl","display_text_range":[0,116],"source":"\u003ca href = \ "http : //11081920.com \"rel = \ "nofollow \"\ u003electal Reform 2017 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 ": {"null "," "id": 42447494, "id_str": "42447494", "name": "Rob", "screen_name": "Unpersuaded112", "location": null, "url": "http://zing.11081920.com ","description ":"Unpersuaded Writer | # 1P1V | 종교는 잃을 것이기도하다. 진실은 절대적이다. 사랑은 파괴 될 수 없다 | "follower_count": 44585, "friends_count": 48600, "listing_count": 231, "follower_count": false, "verified": false, "verified" , "utc_offset": - 14400, "time_zone": "동부 표준시 (US & 캐나다)", "favourites_count": 13496, "statuses_count": 19899, ​​"created_at": "Mon 5 월 25 일 17:34:20 +0000 2009" "geo_enabled": true, "lang": "en", "contributors_enabled": false, "is_translator": false, "profile_background_color": "C0DEED", "profile_background_image_url": "http://pbs.twimg.com/profile_background_images /819010752/fc9331eedc3641643ed63ea99956a01c.jpeg","profile_background_image_url_https":"https://pbs.twimg.com/profile_background_images/819010752/fc9331eedc3641643ed63ea99956a01c.jpeg","profile_background_tile":false,"profile_link_color":"333333","profile_sidebar_border_color ": "C0DEED", "profile_sidebar_fill_color": "DDEEF6", "profile_text_color": "0084B4", "profile_use_background_image": true, "profile_image_url": "http://pbs.twimg.com/profile_images/8927 28364141871104/KCNAgl4R_normal.jpg ","profile_image_url_https ":"https://pbs.twimg.com/profile_images/892728364141871104/KCNAgl4R_normal.jpg ","profile_banner_url ":"https://pbs.twimg.com/profile_banners/42447494/ null ","알림 ": null},"geo ": null,"coordinates ": null,"place ":"default_profile ": false,"default_profile_image ": false,"다음 ": null,"follow_request_sent " null, "기여자": null, "is_quote_status": false, "quote_count": 7, "reply_count": 0, "retweet_count": 87, "favorite_count": 140, "entities": { "hashtags": [{ " 텍스트 ":"트럼프 ","인덱스 ": [4,10]},"urls ": [],"user_mentions ": [{"screen_name ":"realDonaldTrump ","name ":"Donald J. Trump " , "id": 25073877, "id_str": "25073877", "인덱스": [11,27]}], "symbols": [], "media": [{ ""id ": 912754441199243264,"id_str " "912754441199243264", "indices": [117,140], "media_url": "http://pbs.twimg.com/media/DKrBVArXUAAf4F9.jpg", "media_url_https": "https://pbs.twimg.com/media /DKrBVArXUAAf4F9.jpg ","url ":"https://t.co/mVfe6OYwxl","display_url":"pic.twitter.com/mVfe6OYwxl "," 사진 ","크기 ": {"큰 ": {"w ": 600,"h ","expanded_url ":"https://twitter.com/Unpersuaded112/status/912754444831543298/photo/1 "," : 573, "resize": "fit"}, "thumb": { "w": 150, "h": 150, "resize": "crop"}, "small": { "w": 600, h ": 573,"resize ":"fit "},"medium ": {"w ": 600,"h ": 573,"resize ":"fit "}}}}}"extended_entities " 미디어 ": [{"id ": 912754441199243264,"id_str ":"912754441199243264 ","인덱스 ": [117,140],"media_url ":"http://pbs.twimg.com/media/DKrBVArXUAAf4F9.jpg " media_url_https ":"https://pbs.twimg.com/media/DKrBVArXUAAf4F9.jpg ","url ":"https://t.co/mVfe6OYwxl ","display_url ":"pic.twitter.com/mVfe6OYwxl " , "expanded_url": "https://twitter.com/Unpersuaded112/status/912754444831543298/photo/1", "type": "photo", "sizes": { "large": { "w": 600, " h ": 573,"resize ":"fit "},"thumb ": {"w ": 150,"h ": 150,"resize ":"crop "},"small ": {"w ": 600 , "h": 573, "resize": "fit"}, "medium": { "w": 600, "h": 573 "resize": "적합"}}}}} " false, "retweeted": false, "possible_sensitive": false, "filter_level": "low", "lang": "en"}, "is_quote_status": false, "quote_coun 0 ","retweet_count ": 0,"favorite_count ": 0,"entities ": {"hashtags ": [{"text ":"트럼프 ","인덱스 ": [24,30 [url] : [{ "screen_name": "Unpersuaded112", "name": "Rob", "id": 42447494, "id_str": "42447494", "indices" : "3,18"}, { "screen_name": "realDonaldTrump", "name": "Donald J. Trump", "id": 25073877, "id_str": "25073877", "indices": [31,47 false ","filter_level ":"low ","lang ":"en ","timestamp_ms ":"1506470558959 "}

+0

는 왜 두 번째는 오류가 발생하는 기대하지 않을 것이다 : https://docs.python.org/2/library/json.html

두 번째 문제는이 같은 여기 같다 : 여기


는 JSON 모듈에 대한 자세한인가? 'with' 문은 그 문장의 컨텍스트에서 실행되는 들여 쓰기 된 블록을 기대합니다. –

+0

오, 아니, 나는 내가 초보자라는 것을 몰랐다. – Brit

답변

2

json.loads()는 json에서 python으로 데이터를 디코딩합니다. 처음 인쇄 할 때 데이터가 json 객체라고 생각합니다. json.loads()를 사용하여 인쇄하기 전에 파이썬 dict로 디코딩합니다.

두 번째로 콜론 ":"뒤에 아무 것도 없기 때문에 오류가 발생합니다. 들여 쓰기 된 코드없이 "if :"라고 말한 것처럼 오류가 발생합니다. SyntaxError: unexpected EOF while parsing

관련 문제