我正在努力获得if pattern in line then underline the Entire line having pattern
但是作为一个新手学习者我不知道怎么能够适应这个伎俩..
以下是我要处理的文件..
# cat mytest.txt
All records went successful
record1
record2
All records went unsuccessful
record3
record4
All records went successful
record5
record6
在我尝试的内容之下,但在符合条件时执行所有行underlined
:
patt_success = False
with open("mytest.txt") as f:
for line in f:
if patt_success:
if "successful" in line:
patt_success = True
else:
patt_success = False
pp = line.rstrip('\n')
print('\033[1m' + 'pp')
#print('\033[0m' + 'pp')
以下是我使用..
的课程顺序 BOLD = '\033[1m'
UNDERLINE = '\033[4m'
期望的输出:
# cat mytest.txt
All records went successful
----------------------------
record1
record2
All records went unsuccessful
record3
record4
All records went successful
---------------------------
record5
record6
答案 0 :(得分:0)
工作版:
UNDERLINE = '\033[4m'
END = '\033[0m'
with open("mytest.txt") as f:
for line in f:
pp = line.rstrip('\n')
if " successful" in line:
print(UNDERLINE + pp + END)
else:
print(pp)