Python Forum
search for a part of strinf from list in another list !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
search for a part of strinf from list in another list !
#1
hello all and good morning ...
im trying to get the user titles names for all windows on the windows system ... i used ctypes to get the titles names ...
but i need to make a list for words to search in in the titles if u found this word in a string then print("its found ")

my code :
import ctypes
from  __builtin__ import any as b_any

EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible

titles = []
def foreach_window(hwnd, lParam):
	if IsWindowVisible(hwnd):
		length = GetWindowTextLength(hwnd)
		buff = ctypes.create_unicode_buffer(length + 1)
		GetWindowText(hwnd, buff, length + 1)
		titles.append(buff.value)
	return True
EnumWindows(EnumWindowsProc(foreach_window), 0)
#print titles
lst = ["Facebook" , "Gmail" , "Yahoo"]

q = set(titles).issubset(lst)
print q 
the titles output :
Quote:[u'', u'Untitled-2.py - Visual Studio Code', u'Facebook - Log In or Sign Up - Mozilla Firefox', u'Microsoft Edge', u'Microsoft Edge', u'Python "SyntaxError: Non-ASCII character \'\\xe2\' in file" - Stack Overflow \u200e- Microsoft Edge', u'Microsoft Edge', u'Microsoft Edge', u'Microsoft Edge', u'Microsoft Edge', u'Microsoft Edge', u'', u'C:\\WINDOWS\\system32\\cmd.exe - powershell', u'Programs and Features', u'C:\\WINDOWS\\system32\\cmd.exe', u'Registry Editor', u'Setup', u'', u'', u'', u'Settings', u'Microsoft Edge', u'Microsoft Edge', u'Microsoft Store', u'Microsoft Store', u'Settings', u'Untitled - Notepad', u'Program Manager']

the Facebook is here its part of this string ( Facebook - Log In or Sign Up - Mozilla Firefox ) but i got false :(
can u help me please
Reply
#2
Something like this,and stop using Python 2 Dodgy
import re

lst = [
    u'', u'Untitled-2.py - Visual Studio Code',
    u'Facebook - Log In or Sign Up - Mozilla Firefox', u'Microsoft Edge',
    u'Microsoft Edge',
    u'Python "SyntaxError: Non-ASCII character \'\\xe2\' in file" - Stack Overflow \u200e- Microsoft Edge',
    u'Microsoft Edge', u'Microsoft Edge', u'Microsoft Edge', u'Microsoft Edge',
    u'Microsoft Edge', u'', u'C:\\WINDOWS\\system32\\cmd.exe - powershell',
    u'Programs and Features', u'C:\\WINDOWS\\system32\\cmd.exe',
    u'Registry Editor', u'Setup', u'', u'', u'', u'Settings',
    u'Microsoft Edge', u'Microsoft Edge', u'Microsoft Store',
    u'Microsoft Store', u'Settings', u'Untitled - Notepad', u'Program Manager'
]
lst_find = ["Facebook" , "gmail" , "Yahoo"]
for item in lst:
    if re.search('|'.join(lst_find), item):
        print(item)
Output:
Facebook - Log In or Sign Up - Mozilla Firefox
Reply
#3
(Oct-28-2018, 10:43 AM)snippsat Wrote: Something like this,and stop using Python 2 Dodgy
import re

lst = [
    u'', u'Untitled-2.py - Visual Studio Code',
    u'Facebook - Log In or Sign Up - Mozilla Firefox', u'Microsoft Edge',
    u'Microsoft Edge',
    u'Python "SyntaxError: Non-ASCII character \'\\xe2\' in file" - Stack Overflow \u200e- Microsoft Edge',
    u'Microsoft Edge', u'Microsoft Edge', u'Microsoft Edge', u'Microsoft Edge',
    u'Microsoft Edge', u'', u'C:\\WINDOWS\\system32\\cmd.exe - powershell',
    u'Programs and Features', u'C:\\WINDOWS\\system32\\cmd.exe',
    u'Registry Editor', u'Setup', u'', u'', u'', u'Settings',
    u'Microsoft Edge', u'Microsoft Edge', u'Microsoft Store',
    u'Microsoft Store', u'Settings', u'Untitled - Notepad', u'Program Manager'
]
lst_find = ["Facebook" , "gmail" , "Yahoo"]
for item in lst:
    if re.search('|'.join(lst_find), item):
        print(item)
Output:
Facebook - Log In or Sign Up - Mozilla Firefox

thank u very much ...
can u explain please what is this line do ( if re.search('|'.join(lst_find), item): )
and why i should use python 3 ?
Reply
#4
(Oct-29-2018, 07:19 AM)evilcode1 Wrote: can u explain please what is this line do ( if re.search('|'.join(lst_find), item): )
>>> lst_find = ["Facebook" , "gmail" , "Yahoo"]
>>> '|'.join(lst_find)
'Facebook|gmail|Yahoo'
In regex | is or,so it will match Facebook or gmail or Yahoo.
>>> import re
>>> 
>>> s = 'hello gmail from Yahoo.This is Facebook.'
>>> re.findall(r'Facebook|gmail|Yahoo', s)
['gmail', 'Yahoo', 'Facebook']
Quote:and why i should use python 3 ?
Python 2 is end of life in a little over a year.
Python 3 is now 10-year old and it fix a lot mistakes that Python 2 had.
Python 2 support is dropped in Django,IPython...ect.
The scientific stack will start drop Python 2 in 2019,like Pandas,Numpy,Matplotlib...ect.

There is lot features that only work in Python 3 Wink
>>> s = 'cool'
>>> p = 'Python'
>>> print(f'There is lot of {s} features that only {p} {2 * 1.8}--> has,like <{f:^15}>')
There is lot of cool features that only Python 3.6--> has,like <   f-string    >
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] list of list with one or more empty one paul18fr 5 2,326 Oct-04-2023, 09:08 AM
Last Post: jeffreyteague
  convert a list of string+bytes into a list of strings (python 3) pacscaloupsu 4 10,741 Mar-17-2020, 07:21 AM
Last Post: markfilan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020