python 函数读取给定行的所有不为空的后代行
python function to read all descendant lines of a given line which are not empty
该函数将采用两个参数(line、txtfile)并且应该 return txtfile 的 line 参数之后的所有不为空的后代行
例如文本文件包含:
line1
line2
line3
line4
line5
函数应该return line1line2
如果line1
作为参数给出或者
line3line4line5
如果 line3
作为参数给出
def fun(line, filename):
file = open(filename, 'r')
output=''
empty=False
for i in file:
if(i.rstrip('\n')==line or empty):
empty=True
output+=i.rstrip('\n')
if(i.rstrip('\n')==''):
empty=False
return output
filename=input("Enter file name")
line=input("Input line")
output=fun(line,filename)
print(output)
该函数将采用两个参数(line、txtfile)并且应该 return txtfile 的 line 参数之后的所有不为空的后代行
例如文本文件包含:
line1
line2
line3
line4
line5
函数应该return line1line2
如果line1
作为参数给出或者
line3line4line5
如果 line3
作为参数给出
def fun(line, filename):
file = open(filename, 'r')
output=''
empty=False
for i in file:
if(i.rstrip('\n')==line or empty):
empty=True
output+=i.rstrip('\n')
if(i.rstrip('\n')==''):
empty=False
return output
filename=input("Enter file name")
line=input("Input line")
output=fun(line,filename)
print(output)