我有以下程序在某个位置的行中搜索字符串,然后输出搜索匹配项。我希望能够选择是否忽略搜索中的大小写并突出显示输出行中的搜索字符串。
import re, os, glob
from colorama import init, Fore, Back, Style
init(autoreset=True)
path = "c:\\temp2"
os.chdir(path)
def get_str(msg):
myinput = input(msg)
while True:
if not bool(myinput):
myinput = input('No Input..\nDo it again\n-->')
continue
else:
return myinput
ext = get_str('Search the files with the following extension...')
find = get_str('Search string...')
case = get_str(' Ignore case of search string? (y/n)...')
print()
if case == "y" or case == "Y":
find = re.compile(find, re.I)
else:
find = find
files_to_search = glob.glob('*.' + ext)
for f in files_to_search:
input_file = os.path.join(path, f)
with open(input_file) as fi:
file_list = fi.read().splitlines()
for line in file_list:
if re.search(find, line):
line = re.sub(find, Fore.YELLOW + find + Fore.RESET, line)
print(f, "--", line)
如果我为案例选择“n”,该程序有效。如果我选择“y”,程序运行时会出现此错误:
Search the files with the following extension...txt
Search string...this
Ignore case of search string? (y/n)...y
Traceback (most recent call last):
File "C:\7. apps\eclipse\1. workspace\learning\scratch\scratchy.py", line 36, in <module>
line = re.sub(find, Fore.YELLOW + find + Fore.RESET, line)
TypeError: Can't convert '_sre.SRE_Pattern' object to str implicitly
如何为“是”案件做这项工作?
答案 0 :(得分:1)
问题与您在re.sub
电话中进行的字符串连接有关。如果Fore.YELLOW + find + Fore.RESET
是已编译的正则表达式对象而不是字符串,则表达式find
无效。
为了解决这个问题,我建议使用正则表达式模式的不同变量名,而不是用于原始字符串:
if case == "y" or case == "Y":
pattern = re.compile(find, re.I)
else:
pattern = find
然后将pattern
作为第一个参数传递给re.sub
。 find
在所有情况下都将保持字符串,因此它将在第二个参数中的表达式中起作用:
line = re.sub(pattern, Fore.YELLOW + find + Fore.RESET, line)