我有一个关于"对于"的问题功能和" json"。 我想从Twitter数据中找到非常具体的英文关键词。 在这里,我将打开JSON文件并从JSON文件中读取行。 我只想在json.load()之前过滤掉只有英文的值 我认为只有 str(" lang" +':' +" en")会返回我的目标数据,但它没有&# 39;吨
with open(filename, "r+") as fi, open(out_file_name, "a") as fo:
for line in fi:
if keyword in line and str("lang"+':'+"en") in line:
fo.write(line + "\n")
我不确定是否可以分享推文的某些部分。但以下只是样本。
{
"created_at":"Mon Jan 05 07:31:31 +0000 2015",
"id":552004450513346560,
"id_str":"552004450513346560",
"text":"RT @PhoneCasesFTB:Here We Go:https:\/\/t.co\/WwdN1a7kqG @PhoneCasesFTB #iPhone6 #iPhone6Cases #bendgate",
"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":23280370,
"id_str":"23280370",
"name":"Ron Thornton",
"screen_name":"RonThornton",
"location":"Plano, Texas",
"url":"http:\/\/www.makingfilms.com",
"description":"Working on the film @GhostOfGoodnite. Starring Billy Zane,Lacey Chabert, Matt Dallas,-New Film 2015",
"protected":false,
"verified":false,
"followers_count":21349,
"friends_count":22227,
"listed_count":297,
"favourites_count":144216,
"statuses_count":174513,
"created_at":"Sun Mar 08 05:27:25 +0000 2009",
"utc_offset":-21600,
"time_zone":"Central Time (US & Canada)",
"geo_enabled":true,
"lang":"en",
"contributors_enabled":false,
"is_translator":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_link_color":"0084B4",
"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\/503419704246300672\/Vfu_xvKg_normal.jpeg",
"profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/503419704246300672\/Vfu_xvKg_normal.jpeg",
"profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/23280370\/1420024669",
"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":"Thu Jan 01 16:29:27 +0000 2015",
"id":550690275455410176,
"id_str":"550690275455410176",
"text":"Here We Go:https:\/\/t.co\/WwdN1a7kqG @PhoneCasesFTB #iPhone6 #iPhone6Cases #bendgate",
"source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\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":2891446054,
"id_str":"2891446054",
"name":"Phone Cases",
"screen_name":"PhoneCasesFTB",
"location":"",
"url":"http:\/\/phonecasesfromthebest.com",
"description":null,
"protected":false,
"verified":false,
"followers_count":1077,
"friends_count":1099,
"listed_count":4,
"favourites_count":226,
"statuses_count":1133,
"created_at":"Mon Nov 24 22:51:00 +0000 2014",
"utc_offset":-28800,
"time_zone":"Pacific Time (US & Canada)",
"geo_enabled":true,
"lang":"en",
"contributors_enabled":false,
"is_translator":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_link_color":"0084B4",
"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\/537015762888581121\/dK5JrMX-_normal.jpeg",
"profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/537015762888581121\/dK5JrMX-_normal.jpeg",
"default_profile":true,
"default_profile_image":false,
"following":null,
"follow_request_sent":null,
"notifications":null
},
"geo":null,
"coordinates":null,
"place":{
"id":"81b8dcbe189773f2",
"url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/81b8dcbe189773f2.json",
"place_type":"country",
"name":"Hungary",
"full_name":"Hungary",
"country_code":"HU",
"country":"Magyarorsz\u00e1g",
"bounding_box":{
"type":"Polygon",
"coordinates":[
[
[16.1136813, 45.737124],
[16.1136813, 48.585233],
[22.8974571, 48.585233],
[22.8974571, 45.737124]
]
]
},
"attributes":{}
},
"contributors":null,
"retweet_count":2,
"favorite_count":1,
"entities":{
"hashtags":[{
"text":"iPhone6",
"indices":[52, 60]
}, {
"text":"iPhone6Cases",
"indices":[61, 74]
}, {
"text":"bendgate",
"indices":[75, 84]
}],
"trends":[],
"urls":[{
"url":"https:\/\/t.co\/WwdN1a7kqG",
"expanded_url":"https:\/\/medium.com\/@iPhone_6_Cases",
"display_url":"medium.com\/@iPhone_6_Cases",
"indices":[12, 35]
}],
"user_mentions":[{
"screen_name":"PhoneCasesFTB",
"name":"Phone Cases",
"id":2891446054,
"id_str":"2891446054",
"indices":[36, 50]
}],
"symbols":[]
},
"favorited":false,
"retweeted":false,
"possibly_sensitive":false,
"filter_level":"low",
"lang":"en"
},
"retweet_count":0,
"favorite_count":0,
"entities":{
"hashtags":[{
"text":"iPhone6",
"indices":[71, 79]
}, {
"text":"iPhone6Cases",
"indices":[80, 93]
}, {
"text":"bendgate",
"indices":[94, 103]
}],
"trends":[],
"urls":[{
"url":"https:\/\/t.co\/WwdN1a7kqG",
"expanded_url":"https:\/\/medium.com\/@iPhone_6_Cases",
"display_url":"medium.com\/@iPhone_6_Cases",
"indices":[31, 54]
}],
"user_mentions":[{
"screen_name":"PhoneCasesFTB",
"name":"Phone Cases",
"id":2891446054,
"id_str":"2891446054",
"indices":[3, 17]
}, {
"screen_name":"PhoneCasesFTB",
"name":"Phone Cases",
"id":2891446054,
"id_str":"2891446054",
"indices":[55, 69]
}],
"symbols":[]
},
"favorited":false,
"retweeted":false,
"possibly_sensitive":false,
"filter_level":"medium",
"lang":"en",
"timestamp_ms":"1420443091406"
}
{
"created_at":"Mon Jan 05 08:57:34 +0000 2015",
"id":552026107294523392,
"id_str":"552026107294523392",
"text":"Patchworks wydaje specjalne 0.4mm szk\u0142o ochronne dla iPhone 6 i 6+ maj\u0105ce zabezpiecza\u0107 przed #bendgate http:\/\/t.co\/eHh3tjSfE5",
"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":435219617,
"id_str":"435219617",
"name":"Tomasz Dunia",
"screen_name":"theto3k",
"location":"Lublin, Poland",
"url":"http:\/\/theto3k.tk",
"description":"Student of Mechatronics at University of Technology in Lublin. New technologies, movies, books, series and games enthusiast.\n~\nMo\u017cna mnie poczyta\u0107 na theto3k.tk",
"protected":false,
"verified":false,
"followers_count":75,
"friends_count":120,
"listed_count":2,
"favourites_count":1608,
"statuses_count":6775,
"created_at":"Mon Dec 12 20:21:08 +0000 2011",
"utc_offset":3600,
"time_zone":"Warsaw",
"geo_enabled":false,
"lang":"pl",
"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":"4A913C",
"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\/524604116752809985\/nI3P7q03_normal.jpeg",
"profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/524604116752809985\/nI3P7q03_normal.jpeg",
"profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/435219617\/1411995456",
"default_profile":false,
"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":[{
"text":"bendgate",
"indices":[93, 102]
}],
"trends":[],
"urls":[{
"url":"http:\/\/t.co\/eHh3tjSfE5",
"expanded_url":"http:\/\/www.theverge.com\/2015\/1\/4\/7491685\/patchworks-itg-edge-iphone-screen-protector-ces-2015",
"display_url":"theverge.com\/2015\/1\/4\/74916\u2026",
"indices":[103, 125]
}],
"user_mentions":[],
"symbols":[]
},
"favorited":false,
"retweeted":false,
"possibly_sensitive":false,
"filter_level":"medium",
"lang":"pl",
"timestamp_ms":"1420448254785"
}
答案 0 :(得分:2)
看起来你需要的条件是
if keyword in line and '"lang":"en"' in line:
你只是简单地拼写了第二部分,而且不正确。