Python如何在一行中找到任何或所有字符串列表,然后打印找到的确切字符串

时间:2015-04-13 21:07:32

标签: python

我是Python初学者,对字典和列表不太好。

我正在尝试在一行中查找某些字符串,如果找到了字符串,则打印该特定字符串。

这是一行:

{  
  "success": true,
  "start": 0,
  "pagesize": 5,
  "total_count": 4,
  "results_html": "\t<div class=\"market_listing_table_header\">\r\n\t\t<div class=\"market_listing_right_cell market_listing_their_price market_sortable_column\" data-sorttype=\"price\">PRICE<span class=\"market_sort_arrow\" style=\"display:none;\"><\/span><\/div>\r\n\t\t<div class=\"market_listing_right_cell market_listing_num_listings market_sortable_column\" data-sorttype=\"quantity\">QUANTITY<span class=\"market_sort_arrow\" style=\"display:none;\"><\/span><\/div>\r\n\t\t\t\t<div class=\"market_sortable_column\" data-sorttype=\"name\"><span class=\"market_listing_header_namespacer\"><\/span>NAME<span class=\"market_sort_arrow\" style=\"display:none;\"><\/span><\/div>\r\n\t<\/div>\r\n\r\n<a class=\"market_listing_row_link\" href=\"http:\/\/steamcommunity.com\/market\/listings\/730\/%E2%98%85%20Karambit%20%7C%20Stained%20%28Factory%20New%29\" id=\"resultlink_0\">\r\n\t<div class=\"market_listing_row market_recent_listing_row market_listing_searchresult\" id=\"result_0\">\r\n\t\t\t\t<img id=\"result_0_image\" src=\"http:\/\/steamcommunity-a.akamaihd.net\/economy\/image\/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZYMUrsm1j-9xgEObwgfEh_nvjlWhNzZCveCDfIBj98xqodQ2CZknz5wOuqzNQhscxbDDKJXSMo75TfqBjUx7cNcWN6x_69IfAjn4oPHZbgkY4wdFpTXDqKEbwj4uB89g_VcKMCKoSO73yW4PG5fDw2rpDzIPE231Q\/62fx62f\" style=\"border-color: #8650AC;\" class=\"market_listing_item_img\" alt=\"\" \/>\t\t\r\n\t\t\t\t<div class=\"market_listing_right_cell market_listing_their_price\">\r\n\t\t\t<span class=\"market_table_value\">\r\n\t\t\t\tStarting at:<br\/>\r\n\t\t\t\t<span style=\"color:white\">CDN&#36; 396.91<\/span>\r\n\t\t\t<\/span>\r\n\t\t\t<span class=\"market_arrow_down\" style=\"display: none\"><\/span>\r\n\t\t\t<span class=\"market_arrow_up\" style=\"display: none\"><\/span>\r\n\t\t<\/div>\r\n\t\t<div class=\"market_listing_right_cell market_listing_num_listings\">\r\n\t\t\t<span class=\"market_table_value\">\r\n\t\t\t\t<span class=\"market_listing_num_listings_qty\">2<\/span>\r\n\t\t\t<\/span>\r\n\t\t<\/div>\r\n\t\t\r\n\t\t\t\t<div class=\"market_listing_item_name_block\">\r\n\t\t\t<span id=\"result_0_name\" class=\"market_listing_item_name\" style=\"color: #8650AC;\">\u2605 Karambit | Stained (Factory New)<\/span>\r\n\t\t\t<br\/>\r\n\t\t\t<span class=\"market_listing_game_name\">Counter-Strike: Global Offensive<\/span>\r\n\t\t<\/div>\r\n\t<\/div>\r\n<\/a>\r\n\r\n<a class=\"market_listing_row_link\" href=\"http:\/\/steamcommunity.com\/market\/listings\/730\/%E2%98%85%20Karambit%20%7C%20Forest%20DDPAT%20%28Factory%20New%29\" id=\"resultlink_1\">\r\n\t<div class=\"market_listing_row market_recent_listing_row market_listing_searchresult\" id=\"result_1\">\r\n\t\t\t\t<img id=\"result_1_image\" src=\"http:\/\/steamcommunity-a.akamaihd.net\/economy\/image\/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZYMUrsm1j-9xgEObwgfEh_nvjlWhNzZCveCDfIBj98xqodQ2CZknz5wOuqzNQhscxbDDKJXSMoy7TfoDTcz_PhvXdC-4_VSKlns4IKSNrkrYdlIF8GGDPLVMlr0vxhriPJcKJSOpCK72STrMm0IRVO1rYe8SeMm\/62fx62f\" style=\"border-color: #8650AC;\" class=\"market_listing_item_img\" alt=\"\" \/>\t\t\r\n\t\t\t\t<div class=\"market_listing_right_cell market_listing_their_price\">\r\n\t\t\t<span class=\"market_table_value\">\r\n\t\t\t\tStarting at:<br\/>\r\n\t\t\t\t<span style=\"color:white\">CDN&#36; 383.20<\/span>\r\n\t\t\t<\/span>\r\n\t\t\t<span class=\"market_arrow_down\" style=\"display: none\"><\/span>\r\n\t\t\t<span class=\"market_arrow_up\" style=\"display: none\"><\/span>\r\n\t\t<\/div>\r\n\t\t<div class=\"market_listing_right_cell market_listing_num_listings\">\r\n\t\t\t<span class=\"market_table_value\">\r\n\t\t\t\t<span class=\"market_listing_num_listings_qty\">1<\/span>\r\n\t\t\t<\/span>\r\n\t\t<\/div>\r\n\t\t\r\n\t\t\t\t<div class=\"market_listing_item_name_block\">\r\n\t\t\t<span id=\"result_1_name\" class=\"market_listing_item_name\" style=\"color: #8650AC;\">\u2605 Karambit | Forest DDPAT (Factory New)<\/span>\r\n\t\t\t<br\/>\r\n\t\t\t<span class=\"market_listing_game_name\">Counter-Strike: Global Offensive<\/span>\r\n\t\t<\/div>\r\n\t<\/div>\r\n<\/a>\r\n\r\n<a class=\"market_listing_row_link\" href=\"http:\/\/steamcommunity.com\/market\/listings\/730\/%E2%98%85%20Karambit%20%7C%20Safari%20Mesh%20%28Factory%20New%29\" id=\"resultlink_2\">\r\n\t<div class=\"market_listing_row market_recent_listing_row market_listing_searchresult\" id=\"result_2\">\r\n\t\t\t\t<img id=\"result_2_image\" src=\"http:\/\/steamcommunity-a.akamaihd.net\/economy\/image\/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZYMUrsm1j-9xgEObwgfEh_nvjlWhNzZCveCDfIBj98xqodQ2CZknz5wOuqzNQhscxbDDKJXSMop5DfhDDQ619NiWui6_rwOPRK-ttbCYON6OdFJG5LZX_HVbgj0ux9th6MMLsbb9XvojnzpbG5bDxK95Ctaz2tSczny\/62fx62f\" style=\"border-color: #8650AC;\" class=\"market_listing_item_img\" alt=\"\" \/>\t\t\r\n\t\t\t\t<div class=\"market_listing_right_cell market_listing_their_price\">\r\n\t\t\t<span class=\"market_table_value\">\r\n\t\t\t\tStarting at:<br\/>\r\n\t\t\t\t<span style=\"color:white\">CDN&#36; 247.19<\/span>\r\n\t\t\t<\/span>\r\n\t\t\t<span class=\"market_arrow_down\" style=\"display: none\"><\/span>\r\n\t\t\t<span class=\"market_arrow_up\" style=\"display: none\"><\/span>\r\n\t\t<\/div>\r\n\t\t<div class=\"market_listing_right_cell market_listing_num_listings\">\r\n\t\t\t<span class=\"market_table_value\">\r\n\t\t\t\t<span class=\"market_listing_num_listings_qty\">2<\/span>\r\n\t\t\t<\/span>\r\n\t\t<\/div>\r\n\t\t\r\n\t\t\t\t<div class=\"market_listing_item_name_block\">\r\n\t\t\t<span id=\"result_2_name\" class=\"market_listing_item_name\" style=\"color: #8650AC;\">\u2605 Karambit | Safari Mesh (Factory New)<\/span>\r\n\t\t\t<br\/>\r\n\t\t\t<span class=\"market_listing_game_name\">Counter-Strike: Global Offensive<\/span>\r\n\t\t<\/div>\r\n\t<\/div>\r\n<\/a>\r\n\r\n<a class=\"market_listing_row_link\" href=\"http:\/\/steamcommunity.com\/market\/listings\/730\/%E2%98%85%20Karambit%20%7C%20Boreal%20Forest%20%28Factory%20New%29\" id=\"resultlink_3\">\r\n\t<div class=\"market_listing_row market_recent_listing_row market_listing_searchresult\" id=\"result_3\">\r\n\t\t\t\t<img id=\"result_3_image\" src=\"http:\/\/steamcommunity-a.akamaihd.net\/economy\/image\/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZYMUrsm1j-9xgEObwgfEh_nvjlWhNzZCveCDfIBj98xqodQ2CZknz5wOuqzNQhscxbDDKJXSMoy7TfqBjU3-9NcVtik8roKFlC249qCLbh-ZdFFTcmCX_WEYQz57Eht0_AOL5zdoCLv3ijgazgJXhHtqW9RkOeZ-uw8ko32nUQ\/62fx62f\" style=\"border-color: #8650AC;\" class=\"market_listing_item_img\" alt=\"\" \/>\t\t\r\n\t\t\t\t<div class=\"market_listing_right_cell market_listing_their_price\">\r\n\t\t\t<span class=\"market_table_value\">\r\n\t\t\t\tStarting at:<br\/>\r\n\t\t\t\t<span style=\"color:white\">CDN&#36; 504.02<\/span>\r\n\t\t\t<\/span>\r\n\t\t\t<span class=\"market_arrow_down\" style=\"display: none\"><\/span>\r\n\t\t\t<span class=\"market_arrow_up\" style=\"display: none\"><\/span>\r\n\t\t<\/div>\r\n\t\t<div class=\"market_listing_right_cell market_listing_num_listings\">\r\n\t\t\t<span class=\"market_table_value\">\r\n\t\t\t\t<span class=\"market_listing_num_listings_qty\">1<\/span>\r\n\t\t\t<\/span>\r\n\t\t<\/div>\r\n\t\t\r\n\t\t\t\t<div class=\"market_listing_item_name_block\">\r\n\t\t\t<span id=\"result_3_name\" class=\"market_listing_item_name\" style=\"color: #8650AC;\">\u2605 Karambit | Boreal Forest (Factory New)<\/span>\r\n\t\t\t<br\/>\r\n\t\t\t<span class=\"market_listing_game_name\">Counter-Strike: Global Offensive<\/span>\r\n\t\t<\/div>\r\n\t<\/div>\r\n<\/a>\r\n"
}

这是我目前的代码 但它似乎没有正常工作

strings = {"%E2%98%85%20Karambit%20%7C%20Safari%20Mesh%20%28Factory%20New%29",
           "%E2%98%85%20Karambit%20%7C%20Boreal%20Forest%20%28Factory%20New%29"}
if any(string in strings for string in line):
    searchString = line.strip(strings)
    print("Found String %s" % searchString)

3 个答案:

答案 0 :(得分:0)

而不是:

if any(string in strings for string in line):

尝试:

if any(string in strings for string in line.values()):

这样,您就会搜索line字典中的值(例如5),而不是搜索密钥(例如"pagesize")。

另外,Python的布尔值为True,而不是true

答案 1 :(得分:0)

您的line似乎是一本字典,但是您有true,这让我觉得它是JSON?或者你的意思是True,Python布尔值?我将假设后者(这是一个Python字典,你的意思是True)。非常简单,字典是键/值对的无序集合。如果我想要与"results_html"相关联的值,我会使用line['results_html']来获取它。我也会假设您有兴趣查看"results_html"密钥,因为这是唯一看起来相关的密钥。

有些人可能会建议使用正则表达式,但出于您的目的,我认为这就像用轨道激光器开水一样 - 过度杀伤。

你有一些字符串集合,你想知道这些字符串是否出现在字典中。这可以通过以下方式完成:

for string in strings:
    if string in line.get('results_html', ''):
        print(string)

如果字典恰好没有get密钥,我在这里使用KeyError方法来避免'results_html'。你也可以将它浓缩成一个理解:

result = [string for string in strings if string in line.get('results_html', '')

这就是你要找的东西吗?

答案 2 :(得分:0)

这是正则表达式模块设计的确切问题。正则表达式允许快速轻松的文本模式识别。

import re

line = ""

x = re.findall('(%E2%98%85%20Karambit%20%7C%20(Safari%20Mesh|Boreal%20Forest)?%20%28Factory%20New%29)', line)

for i in x:
   print (i[0])

仅使用正则表达式'或'运算符'|'搜索您想要的两种情况。

或者你可以使用通配符操作符,它不会区分字符串中的单词。

x = re.findall('(%E2%98%85%20Karambit%20%7C%20.*?%20.*?%20%28Factory%20New%29)', line)

for i in x:
    print (i)

这将找到您想要的两个字符串,并找到具有类似格式的其他字符串,例如:

%E2%98%85%20Karambit%20%7C%20European%20Arctic%20%28Factory%20New%29

可以在此处找到正则表达式模块的文档:

https://docs.python.org/3.4/library/re.html