簡體   English   中英

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

[英]Python how to find any or all strings-list in a line and then print the exact string found

我是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)

代替:

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 ,不是true

您的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)

如果字典碰巧沒有'results_html'鍵,我在這里使用get方法來避免KeyError 您也可以將其簡化為一個理解:

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

那是您要找的東西嗎?

這是正則表達式模塊設計的確切類型的問題。 正則表達式允許快速輕松地識別文本模式。

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

regex模塊的文檔可以在這里找到:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM