2017-10-25 5 views
-2

誰かがこのような形式のコードが動作するのはなぜですか?json形式のつぶやきをPythonに読み込むのに問題があります

私は

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' 

>

を取得

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 

しかし、私はこれを書いている場合

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"]) 

すべては理由があり、結構なのですか?また、なぜ上記のループでは、同じ変数、つまりdata_in_fileにdata_in_fileが含まれている必要がありますか?なぜ書くことができないのですか?

for x in data_in_file: 

??

JSONファイルは次のようになりますのでご注意ください。

{ "のcreated_at": "水9月27日午前0時02分37秒0000 2017"、 "ID":912829797428056065、 "id_str": "912829797428056065"、 "テキスト": "RT @S_T_O_P_TERROR:シーラ・ジャクソン・リー議員#NFLプレイヤーをサポートするためにハウスフロアに膝を立てる\ n#"息子をひざまずくトランプ・コール・プレイヤー "、" source ":" \ u003ca href = "のhttp://twitter.com/download/android \" のrel = \ "nofollowを\" \ u003eTwitterアンドロイドの\ u003c用/の\ u003e」、 "切り捨て":偽、 "in_reply_to_status_id":ヌル、 "in_reply_to_status_id_str":ヌル、 "in_reply_to_user_id":ヌル、 "in_reply_to_user_id_str":ヌル、 "in_reply_to_screen_name":NULL、 "ユーザ":{ "ID":38351186、 "id_str": "38351186"、 "名前": "際どい"、 "SCREEN_NAME": "RacySicilian"、 "location": "Everywhere"、 "url": "http://gab.ai/RacySicilian"、 "description": "Constitutio nalist保守的なキリスト教の#BackTheBlue #ProLife #NRA#2ADefenders #Military #SOT #IStandWithIsrael #ProIsrael #BanIslam #Bacon #Infidel #BlueHand " "translator_type": "なし"、 "保護":偽、 "検証":偽、" 「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」 "東部標準時(米国&カナダ)"、 "geo_enabled":::、 "TIME_ZONE" -14400偽、 "LANG": "EN"、 "contributors_enabled":偽、 "is_translator":偽、 "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":偽、 "profile_link_color": "DD2E44"、 "profile_sidebar_border_color": "000000"、 "profile_sidebar_fill_color": "000000"、 "profile_text_color": "000000"、 "profile_use_background_image":偽、" profile_image_url ":" http://pbs.twimg。 com/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/ 「follow_request_sent」:null、「notifications」:null}、「geo」:null、「coordinates」:null、「null」、「notify」、 "場所":ヌル、 "貢献":ヌル、 "retweeted_status" { "のcreated_at": "火9月26日夜11時33分十三秒+0000 2017"、 "ID":912822395026722816、 "id_str": "912822395026722816"、」テキスト ":" Rep。 Sheila Jackson Leeは#NFLプレイヤーをサポートするために家の床に忍び込んでいます。\ n#\ u2026 https://t.co/ozifjD9TXj",display_text_range::[0,140],"source "の息子をひざまずけるトランプコールプレイヤー: "\ 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この場合、nullを返します。 ID:2976578589、 "id_str": "2976578589"、 "名前": "STOP TERROR"、 "screen_name": "S_T_O_P_TERROR"、 "location": "ワールドワイド"、 "in_reply_to_screen_name" "情報" 4:テロリズムの恐怖自由民主主義自由民主主義自由化自由声明腐敗&アーミングテロリズムボコハラムISISハマスアルカイダトルコイランクルド "、" translator_type ":" none "、" protected ": false、 "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 ":"太平洋時間(US &カナダ) "、" 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_sidebar_border_color": "F2E195"、 "profile_sidebar_fill_color": "FFF7CC"、 "profile_text_color": "0C3E53"、 "profile_use_background_image":true、 "profile_image_url": "profile_background_tile":false、 "profile_link_color" 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_image ":偽、"フォロー "、" null、 "coordinate":null、 "place":null、 "contributors":null、 "is_quote_status":false、 "extended_tweet":{"full_text": "Rep。 Sheila Jackson Leeは#NFLプレイヤーをサポートするために家の床に忍び込んでいます。\ n# '雌の息子をひざまずけるトランプの選手は\ "#人種差別。\" https://t.co/UE04GvqHhP","display_text_range " :{"テキスト": "トランプ"、 "インデックス":[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 、 "h":180、 "resize": "fit"}}、 "video_info":{"aspect_ratio"、 "h":150、 "resize": "crop"}、 ":[3,2]、" duration_millis ":154000、" variant ": [{"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/ ""、 "タイプ": "ビデオ"、 "サイズ":{"小":{"w":270、 "UE04GvqHhP"、 "expanded_url": "https://twitter.com/S_T_O_P_TERROR/status/912822395026722816/video/1" 「サイズ」、「サイズ」、「サイズ」、「サイズ」、「サイズ」、「サイズ」、「サイズ」: :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/ "retweet_count":5、 "favorite_count":3、 "entities":{"ハッシュタグ":[ {"テキスト": "索引": "インデックス":[60,64]}、{"テキスト": "トランプ"、 "インデックス":[73,79]}、 "URL" : "https://t.co/ozifjD9TXj"、 "expanded_url": "https://twitter.com/i/web/status/912822395026722816"、 "display_url": "twitter。[]、[記号]:[]}、[お気に入り]:偽、[retweeted]:偽、 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"、 "インデックス" ":" [3,18]} "、"記号 ":[]}、"お気に入り ":false、" retweeted ":false、" filter_level ":" low "、" lang ":" en "、" timestamp_ms " "id": "912829800338935808"、 "text": "。" @ realDonaldTrump @USUN素晴らしい! "" 1506470557921 "} {" created_at ":" Wed Sep 27 00:02:38 +0000 2017 "、" id ":912829800338935808" id_str " \ ud83d \ udc4d @nikkihaleyはすばらしい選択でした4 #USA Ambassador 2 #UN! \ u003c href = \ "http://www.twitter.com \" rel = \ "nofollow \" \ u003e BlackBerry用BlackBerry \ u003c/a \ u003e "\ ud83d \ ude03 #Trump #MAGA"、 "source" "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" in_reply_to_status_id " "、" 717398291571412992 "、" id_str ":" 717398291571412992 "、"名前 ":"真実政治 "、" screen_name ":" truthinppt "、" location ":" United Kingdom "、" url ":null、" description "左派、リベラル、スコット・ナッツ、EU、PC、MSM、人種差別主義者、根本主義者・無神論者によって嘘をついたのはうんざりだ& anti #Trump & anti #Brexitの狂信的な人物、 "翻訳者のタイプ": "none"、 "protected" :false、 "verified":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 ":nu 「false」、「lang」:「en」、「contributors_enabled」:false、「is_translator」:false、「profile_background_color」:「000000」、「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_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 null "、"通知 ":null}、" geo ":null、" coordinates ":null、" place ":null"、 "default_profile":false、 "default_profile_image":false、 "following":null、 "follow_request_sent" 、 "contributors":null、 "is_quote_status":false、 0、 "reply_count":0、 "retweet_count":0、 "favorite_count":0、 "entities":{"ハッシュタグ":[{"text": "USA"、 "indices":[72,76 {90,93}}、{"text": "Trump"、 "indices":[97,103]}、{"text": "MAGA" "{" "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 " "low"、 "lang": "en"、 "timestamp_ms": "1506470558615" "false"、 "retweeted":false、 "filter_level": "low"、 "lang" } {"created_at": "Wed Sep 27 00:02:38 +0000 2017"、 "id":912829801781657606、 "id_str": "912829801781657606"、 "text": "RT @ Unpersuaded112:Mr.#Trump @realDonaldTrumpあなたが小さな人のためのキャンペーンでやると言ったことは、明らかに嘘でした。 \ u003c href = \ "http://twitter.com/download/iphone \" rel = \ "nofollow \" \ u003e iPhone用の\ u003c/a \ u003e "、"ソース " "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 、 "検証済み":false、 "followers_count":79、 "friends_count":259、 "listing_count":2、 "favourites_count":3751、 "statuses_count":4735、 "created_at": "Sat Oct 08 16:59:03 「false」、「profile_background_color」:「F5F8FA」:false、「lang」:「en」、「contributors_enabled」:false、「is_translator」:false、「profile_background_color」:「F00002020」、「utc_offset」:null、「time_zone」: "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、 "通知":null、 "geo":null、 "座標":null、 "場所":null、 "コントリビュータ":null、 "retweeted_status":{"created_at": "null"、 "not_request_sent" Tue Sep 26 19:03:12 +0000 2017 "、" id ":912754444831543298、" id_str ":" 912754444831543298 "、" text ":" Mr。 #Trump @realDonaldTrumpあなたが小さな人のためのキャンペーンでやると言ったことは、明らかに嘘でした。 https://t.co/mVfe6O​​Ywxl",display_text_range":[0116],"source":"\u003ca href = \ "http://11081920.com" "rel = \" nofollow \ "\ u003e選挙改革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 ":{V46 \ "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」:44585、「friends_count」:48600、 「お気に入り」、「お気に入り」、「favourites_count」:13496、「statuses_count」:19899、「created_at」:「Mon May 25 17:34:20 +0000 2009」、「utc_offset」: - 14400、「time_zone」:「東部時間(US &カナダ) "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 ":" http://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、「follow_request_sent」:null、「通知」:null}、「geo」:null、「coordinates」:null、「place」:「default_profile」:false、「default_profile_image」:false "寄付者":null、 "is_quote_status":偽、 "quote_count":7、 "reply_count":0、 "retweet_count":87、 "favorite_count":140、 "entities":{"hashtags":[{" "{" "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 ":" http://t.co/mVfe6O​​Ywxl "、" display_url ":" pic.twitter.com/mVfe6O​​Ywxl "、" 「拡大」:「https://twitter.com/Unpersuaded112/status/912754444831543298/photo/1」、「タイプ」:「写真」、「サイズ」:{"大":{"w":600、 "h" : "w":150、 "サイズ変更": "切り抜き"}、 "小": 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/mVfe6O​​Ywxl "、" display_url ":" pic.twitter.com/mVfe6O​​Ywxl " 、 "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": "fit"}}}}} "favorited": false、 "retweeted":false、 "possibly_sensitive":false、 "filter_level": "low"、 "lang": "en"}、 "is_quote_status":false、 "quote_coun 0 "、" reply_count ":0、" retweet_count ":0、" favorite_count ":0、" entities ":{"ハッシュタグ ":[{" text ":"トランプ "、"インデックス ":[24,30 [{"スクリーン名": "Unpersuaded112"、 "名前": "Rob"、 "id":42447494、 "id_str": "42447494"、 "indices"} "、" urls " : "[3,18]}、{" screen_name ":" realDonaldTrump "、" name ":" Donald J. Trump "、" id ":25073877、" id_str ":" 25073877 "、" indices ":[31,47 ]、[記号]:[]}、[お気に入り]:false、 "retweeted":false、 "filter_level": "low"、 "lang": "en"、 "timestamp_ms": "1506470558959"}

+0

なぜあなたは二番目がエラーを起こすことを期待しないでしょうか? 'with'ステートメントは、そのステートメントの文脈で実行されるインデントされたブロックを期待します。 –

+0

ああ、私は初心者だとは気付かなかった – Brit

答えて

2

json.loads()は、jsonからpythonにデータをデコードします。最初に印刷しようとすると、データはjsonオブジェクトです。 json.loads()を使用すると、それを印刷する前にPythonのdictにデコードします。

エラーが2回発生するのは、コロン「:」の後に何も付いていないためです。インデントされたコードがなくても "if:"と言うように、エラーが発生します。ここで


は、JSONモジュールの詳細については次のとおりです。https://docs.python.org/2/library/json.html

第二の問題は、ここと同じです:SyntaxError: unexpected EOF while parsing

関連する問題