Text file:
InheritedFrom: abc@aol.com
InheritedAltFrom: abc@aol.com
From: CN=deepak sethi/O=MHI
INetFrom: xwy.com
code I am using to extract line containing "From:" only
import re
with open('abc.txt', 'r') as file:
raw = file.readlines()
for line in raw :
if re.search(r'/b' + "From:" + r'/b', line):
print (line)
expecting :-
From: CN=deepak sethi/O=MHI
I dont understand what's going wrong?
Regexp's word boundary is presented with \\b
, not /b
:
with open('abc.txt', 'r') as f:
for l in f.readlines():
if re.search(r'\bFrom\b', l):
print(l)
The output:
From: CN=deepak sethi/O=MHI
import re
with open('abc.txt', 'r') as file:
raw = file.readlines()
for line in raw :
if re.search(r'^From:', line):
print line
Will solve your problem
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.