如何使用python查找文件中的精确长度和匹配字符串
我有一个文件如何使用python查找文件中的精确长度和匹配字符串,python,Python,我有一个文件 "Starting program and Starting program Loading molecule... Initialising variables... Starting the calculation - this could take a while! Molecule energy = 2432.6 kcal mol-1 Calculation finished. Bye!" import sys import re search_string = "St
"Starting program and
Starting program
Loading molecule...
Initialising variables...
Starting the calculation - this could take a while!
Molecule energy = 2432.6 kcal mol-1
Calculation finished. Bye!"
import sys
import re
search_string = "Starting program"
txtlength=len(search_string)
print "txtlength",txtlength
lines = open( "C:\search.txt", "r" ).readlines()
for line in lines:
if re.search( search_string, line ):
print line,
else :
print "Not found"
我只在文件中查找第2行,但来自此代码的输出是1行,同时显示的是您不需要正则表达式,例如:
with open("C:/search.txt") as inp:
for line in inp:
if line.strip() == search_string:
print line
请告诉我如何给出代码中的大写和小写字母。检查是否相等时,可以使用
.lower()
将两个字符串转换为小写。