我想在字符串中搜索通配符。搜索python中包含通配符的字符串
這是我正在使用的代碼。
with open('file0001.mbx', 'r') as infile,open("ESAL_V.txt",'a') as ESAL_V, open("ESAL.txt", 'a') as ESAL:
for line in infile:
if (str('ESAL15973P'))in line:
ESAL_V.write(line)
elif line.startswith("ESAL"):
ESAL.write(line)
我想用通配符替換字符串搜索中的幾個字符。這是我想要搜索的
if (str('ESAL*****P'))in line:
但這不起作用。如何在我的字符串搜索中包含這些通配符?
當然這是行不通的;即尋找字面星號,Python字符串不包含通配符的概念。您需要切換到[正則表達式](https://docs.python.org/2/howto/regex.html)。 – jonrsharpe 2014-09-05 14:00:01