-2

Can someone explain to me why one of these forms of code works and the other doesn't?

when i write

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

I get the error

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

>

if I write

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

I get an error of

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

But if I write this

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

everything is fine and dandy, why is that? Also why in the one above does the for loop need to contain the same variable i.e. data_in_file in data_in_file? why can't i write

for x in data_in_file: 

??

Please note the json file looks like this:

{"created_at":"Wed Sep 27 00:02:37 +0000 2017","id":912829797428056065,"id_str":"912829797428056065","text":"RT @S_T_O_P_TERROR: Rep. Sheila Jackson Lee Kneels On House Floor In Support Of #NFL Players\n#Trump calling players who kneel a 'son of\u2026 ","source":"\u003ca href=\"http://twitter.com/download/android\" rel=\"nofollow\"\u003eTwitter for Android\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":38351186,"id_str":"38351186","name":"Racy","screen_name":"RacySicilian","location":"Everywhere","url":"http://gab.ai/RacySicilian","description":"Constitutionalist Conservative Christian #BackTheBlue #ProLife #NRA #2ADefenders #Military #SOT #IStandWithIsrael #ProIsrael #BanIslam #Bacon #Infidel #BlueHand","translator_type":"none","protected":false,"verified":false,"followers_count":24866,"friends_count":23127,"listed_count":203,"favourites_count":88365,"statuses_count":106639,"created_at":"Thu May 07 02:55:41 +0000 2009","utc_offset":-14400,"time_zone":"Eastern Time (US & Canada)","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.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/profile_banners/38351186/1480972281","default_profile":false,"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 23:33:13 +0000 2017","id":912822395026722816,"id_str":"912822395026722816","text":"Rep. Sheila Jackson Lee Kneels On House Floor In Support Of #NFL Players\n#Trump calling players who kneel a 'son of\u2026 https://t.co/ozifjD9TXj","display_text_range":[0,140],"source":"\u003ca href=\"https://studio.twitter.com\" rel=\"nofollow\"\u003eMedia Studio\u003c/a\u003e","truncated":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,"in_reply_to_screen_name":null,"user":{"id":2976578589,"id_str":"2976578589","name":"STOP TERROR","screen_name":"S_T_O_P_TERROR","location":"Worldwide","url":null,"description":"Info 4 Terrorism Tyranny Freedom Democracy Liberty Freespeech Corruption to STOP funding & arming TERROR Boko Haram ISIS Hamas Al Qaeda Turkey Iran Kurds","translator_type":"none","protected":false,"verified":false,"followers_count":15963,"friends_count":2619,"listed_count":501,"favourites_count":21369,"statuses_count":96487,"created_at":"Tue Jan 13 15:34:13 +0000 2015","utc_offset":-25200,"time_zone":"Pacific Time (US & Canada)","geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"BADFCD","profile_background_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_tile":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. Sheila Jackson Lee Kneels On House Floor In Support Of #NFL Players\n#Trump calling players who kneel a 'son of a bitch' is \"#racism.\" https://t.co/UE04GvqHhP","display_text_range":[0,138],"entities":{"hashtags":[{"text":"NFL","indices":[60,64]},{"text":"Trump","indices":[73,79]},{"text":"racism","indices":[129,136]}],"urls":[],"user_mentions":[],"symbols":[],"media":[{"id":912821680266928128,"id_str":"912821680266928128","indices":[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":150,"resize":"crop"},"medium":{"w":270,"h":180,"resize":"fit"}},"video_info":{"aspect_ratio":[3,2],"duration_millis":154000,"variants":[{"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","indices":[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":150,"resize":"crop"},"medium":{"w":270,"h":180,"resize":"fit"}},"video_info":{"aspect_ratio":[3,2],"duration_millis":154000,"variants":[{"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"}]}}]}},"quote_count":0,"reply_count":2,"retweet_count":5,"favorite_count":3,"entities":{"hashtags":[{"text":"NFL","indices":[60,64]},{"text":"Trump","indices":[73,79]}],"urls":[{"url":"https://t.co/ozifjD9TXj","expanded_url":"https://twitter.com/i/web/status/912822395026722816","display_url":"twitter.com/i/web/status/9\u2026","indices":[117,140]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en"},"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"NFL","indices":[80,84]},{"text":"Trump","indices":[93,99]}],"urls":[],"user_mentions":[{"screen_name":"S_T_O_P_TERROR","name":"STOP TERROR","id":2976578589,"id_str":"2976578589","indices":[3,18]}],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1506470557921"} {"created_at":"Wed Sep 27 00:02:38 +0000 2017","id":912829800338935808,"id_str":"912829800338935808","text":".@realDonaldTrump @USUN Superb! \ud83d\udc4d @nikkihaley was an excellent choice 4 #USA Ambassador 2 #UN! \ud83d\ude03 #Trump #MAGA","source":"\u003ca href=\"http://www.twitter.com\" rel=\"nofollow\"\u003eTwitter for BlackBerry\u003c/a\u003e","truncated":false,"in_reply_to_status_id":912825663694016522,"in_reply_to_status_id_str":"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":"Truth In Politics","screen_name":"truthinppt","location":"United Kingdom","url":null,"description":"Sick of being lied to by leftists, liberals, Scots Nats, the EU, the PC, the MSM, racists, fundamentalist theists/atheists & anti #Trump & anti #Brexit fanatics","translator_type":"none","protected":false,"verified":false,"followers_count":1727,"friends_count":1695,"listed_count":16,"favourites_count":14272,"statuses_count":3284,"created_at":"Tue Apr 05 17:07:36 +0000 2016","utc_offset":null,"time_zone":null,"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/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,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"USA","indices":[72,76]},{"text":"UN","indices":[90,93]},{"text":"Trump","indices":[97,103]},{"text":"MAGA","indices":[104,109]}],"urls":[],"user_mentions":[{"screen_name":"realDonaldTrump","name":"Donald J. Trump","id":25073877,"id_str":"25073877","indices":[1,17]},{"screen_name":"USUN","name":"US Mission to the UN","id":249677516,"id_str":"249677516","indices":[18,23]},{"screen_name":"nikkihaley","name":"Nikki Haley","id":37666984,"id_str":"37666984","indices":[34,45]}],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1506470558615"} {"created_at":"Wed Sep 27 00:02:38 +0000 2017","id":912829801781657606,"id_str":"912829801781657606","text":"RT @Unpersuaded112: Mr. #Trump @realDonaldTrump everything you said you would do in the campaign for little people was apparently a lie. ht\u2026","source":"\u003ca href=\"http://twitter.com/download/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\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":784800285059387392,"id_str":"784800285059387392","name":"cookie","screen_name":"Redheaddiva79","location":null,"url":null,"description":null,"translator_type":"none","protected":false,"verified":false,"followers_count":79,"friends_count":259,"listed_count":2,"favourites_count":3751,"statuses_count":4735,"created_at":"Sat Oct 08 16:59:03 +0000 2016","utc_offset":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_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 everything you said you would do in the campaign for little people was apparently a lie. https://t.co/mVfe6OYwxl","display_text_range":[0,116],"source":"\u003ca href=\"http://11081920.com\" rel=\"nofollow\"\u003eElectoral Reform 2017 V46\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":42447494,"id_str":"42447494","name":"Rob","screen_name":"Unpersuaded112","location":null,"url":"http://zing.11081920.com","description":"Unpersuaded Writer | #1P1V | Religion is a bet some will lose | Truth is absolute | Love can not be destroyed | Time is a human creation, use it wisely","translator_type":"none","protected":false,"verified":false,"followers_count":44585,"friends_count":48600,"listed_count":231,"favourites_count":13496,"statuses_count":19899,"created_at":"Mon May 25 17:34:20 +0000 2009","utc_offset":-14400,"time_zone":"Eastern Time (US & Canada)","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/892728364141871104/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/1497923909","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,"quote_count":7,"reply_count":0,"retweet_count":87,"favorite_count":140,"entities":{"hashtags":[{"text":"Trump","indices":[4,10]}],"urls":[],"user_mentions":[{"screen_name":"realDonaldTrump","name":"Donald J. Trump","id":25073877,"id_str":"25073877","indices":[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","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"}}}]},"extended_entities":{"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","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_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"Trump","indices":[24,30]}],"urls":[],"user_mentions":[{"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]}],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1506470558959"}

Brit
  • 23
  • 1
  • 8

1 Answers1

2

json.loads() decodes the data from json to python. I think when you try to print it the first time, the data is a json object. By using json.loads() you decode it to a Python dict before printing it.

The second time you get an error is because you don't have anything after the colon ":". Just as if it were to say "if:" without a following indented code, you get an error.


Here is more about the json module: https://docs.python.org/2/library/json.html

The second problem is the same as here: SyntaxError: unexpected EOF while parsing

Eric B
  • 152
  • 1
  • 11