import openpyxl
import os
import re
os.chdir('C:\\Users\\Rakesh Kumar\\Desktop')
wb = openpyxl.load_workbook('example.xlsx')
sheet = wb['test']
c=sheet['G29'].value
with open("test.txt","r") as f:
lines = f.read().rstrip()
match = re.search(str(c), str(lines))
if match:
print (c)
t= match.end(0)
i = f.seek(t+1)
nxtlines = f.readline()
print(nxtlines)
print(i)
print(t)
********输出***************** ***********************
PM-HR-PC
rhrhrhr PM-HR-PC
129
128
**********************的test.txt ********************** ********
dsfsdffsfsd
fsdfs
sdfsfs
dfdsfsdf
sfsdf
sdfsd
SFSF
SF
SF
取值
SFS
FS
FSD
fsdfsdfsfsdfs
SDFS
fsdfsdfergretgrghjt
小时
rhrhrhr PM-HR-PC
宿主
宿主
宿主
********************************** example.xlxs ********** **********************
Coloum G29是" PM-HR-PC"
答案 0 :(得分:0)
不要将你的行读成一个大字符串并与之匹配lines = f.read().rstrip()
,而是直接迭代这些行并匹配每一行。
with open(path) as fh:
for line in fh.read().splitlines():
if re.match(REGEX, line):
function(line)