Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为什么不是';t我在python中使用JSON进行解析_Python 3.x - Fatal编程技术网

Python 3.x 为什么不是';t我在python中使用JSON进行解析

Python 3.x 为什么不是';t我在python中使用JSON进行解析,python-3.x,Python 3.x,我想提取特定的数据/值,但不断出现以下错误: with open('tweets.json') as json_data: data = json.load(json_data) print(data['text']) 我是python的初学者,我正在尝试使用TwitterAPI进行学习 这是我的json: print(data['text']) TypeError: string indices must be integers 数据为文本格式。(不是JSO

我想提取特定的数据/值,但不断出现以下错误:

with open('tweets.json') as json_data:
        data = json.load(json_data)
        print(data['text'])
我是python的初学者,我正在尝试使用TwitterAPI进行学习

这是我的json:

print(data['text'])
TypeError: string indices must be integers
数据为文本格式。(不是JSON格式)。因此,首先必须将其转换为JSON

"{\"created_at\":\"Wed Feb 03 03:02:04 +0000 2016\",\"id\":694717462621884416,\"id_str\":\"694717462621884416\",\"text\":\"Finallyy @taylorcaniff Happy bday bae, I love you soooo much, keep smiling, I'm so proud of everything you've done\\u2661 https:\\/\\/t.co\\/uwjeASxsA3\",\"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\":1364125758,\"id_str\":\"1364125758\",\"name\":\"C o l l i n e r\",\"screen_name\":\"HoodsPizzaxJCat\",\"location\":\"2\\/5 UJ | The Vamps DM\",\"url\":null,\"description\":\"\\u25a8Issa liked x2 & follow\\u25a8Brent Follow, liked x4 &DM\\u25a8Chris liked x3 and follows\\u25a8Taylor, Kizzy, Jacob, Caspar, King B. & Momma Collins follow\\u25a8Trevor Liked \\u25a8\",\"protected\":false,\"verified\":false,\"followers_count\":12136,\"friends_count\":13282,\"listed_count\":20,\"favourites_count\":29245,\"statuses_count\":46864,\"created_at\":\"Fri Apr 19 10:59:10 +0000 2013\",\"utc_offset\":-10800,\"time_zone\":\"Buenos Aires\",\"geo_enabled\":true,\"lang\":\"es\",\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"09ED92\",\"profile_background_image_url\":\"http:\\/\\/pbs.twimg.com\\/profile_background_images\\/506799872326893569\\/vdaHWDTj.jpeg\",\"profile_background_image_url_https\":\"https:\\/\\/pbs.twimg.com\\/profile_background_images\\/506799872326893569\\/vdaHWDTj.jpeg\",\"profile_background_tile\":true,\"profile_link_color\":\"4CC74C\",\"profile_sidebar_border_color\":\"FFFFFF\",\"profile_sidebar_fill_color\":\"DDEEF6\",\"profile_text_color\":\"333333\",\"profile_use_background_image\":true,\"profile_image_url\":\"http:\\/\\/pbs.twimg.com\\/profile_images\\/688994368057921536\\/IKy-2UYn_normal.jpg\",\"profile_image_url_https\":\"https:\\/\\/pbs.twimg.com\\/profile_images\\/688994368057921536\\/IKy-2UYn_normal.jpg\",\"profile_banner_url\":\"https:\\/\\/pbs.twimg.com\\/profile_banners\\/1364125758\\/1450566712\",\"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,\"retweet_count\":0,\"favorite_count\":0,\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[{\"screen_name\":\"taylorcaniff\",\"name\":\"Taylor Caniff\",\"id\":1396698397,\"id_str\":\"1396698397\",\"indices\":[9,22]}],\"symbols\":[],\"media\":[{\"id\":694717457911693312,\"id_str\":\"694717457911693312\",\"indices\":[116,139],\"media_url\":\"http:\\/\\/pbs.twimg.com\\/media\\/CaQh2OIWwAA6G_C.jpg\",\"media_url_https\":\"https:\\/\\/pbs.twimg.com\\/media\\/CaQh2OIWwAA6G_C.jpg\",\"url\":\"https:\\/\\/t.co\\/uwjeASxsA3\",\"display_url\":\"pic.twitter.com\\/uwjeASxsA3\",\"expanded_url\":\"http:\\/\\/twitter.com\\/HoodsPizzaxJCat\\/status\\/694717462621884416\\/photo\\/1\",\"type\":\"photo\",\"sizes\":{\"large\":{\"w\":480,\"h\":800,\"resize\":\"fit\"},\"thumb\":{\"w\":150,\"h\":150,\"resize\":\"crop\"},\"small\":{\"w\":340,\"h\":566,\"resize\":\"fit\"},\"medium\":{\"w\":480,\"h\":800,\"resize\":\"fit\"}}}]},\"extended_entities\":{\"media\":[{\"id\":694717457911693312,\"id_str\":\"694717457911693312\",\"indices\":[116,139],\"media_url\":\"http:\\/\\/pbs.twimg.com\\/media\\/CaQh2OIWwAA6G_C.jpg\",\"media_url_https\":\"https:\\/\\/pbs.twimg.com\\/media\\/CaQh2OIWwAA6G_C.jpg\",\"url\":\"https:\\/\\/t.co\\/uwjeASxsA3\",\"display_url\":\"pic.twitter.com\\/uwjeASxsA3\",\"expanded_url\":\"http:\\/\\/twitter.com\\/HoodsPizzaxJCat\\/status\\/694717462621884416\\/photo\\/1\",\"type\":\"photo\",\"sizes\":{\"large\":{\"w\":480,\"h\":800,\"resize\":\"fit\"},\"thumb\":{\"w\":150,\"h\":150,\"resize\":\"crop\"},\"small\":{\"w\":340,\"h\":566,\"resize\":\"fit\"},\"medium\":{\"w\":480,\"h\":800,\"resize\":\"fit\"}}}]},\"favorited\":false,\"retweeted\":false,\"possibly_sensitive\":false,\"filter_level\":\"low\",\"lang\":\"en\",\"timestamp_ms\":\"1454468524972\"}\r\n"

显示
tweets.json
中的内容?似乎
数据
是字符串而不是字符串dictionary@d2207197我怎样才能解决这个问题?因此,我将数据保存为dictionary而不是string,我如何为多行保存数据?因为现在我只保存了一条tweet。但是我想用多个tweet数据填充json,并逐个读取它们。可以这样做吗?在这种情况下,您可以在列表中获取数据,即字符串列表。您可以在列表上迭代,并可以找到每个sting的“文本”。对于字符串列表中的单个字符串:print json.loads(data)[“text”]
>>> json.loads(data)["text"]
u"Finallyy @taylorcaniff Happy bday bae, I love you soooo much, keep smiling, I'm so proud of everything you've done\u2661 https://t.co/uwjeASxsA3"