我正在测试给定文件的输入/输出。每个文件都包含字符串,我将搜索' s'在该文件中,返回该文件中s的有序列表。该功能不是给出位置,而是给出整行。为什么呢?
这是我的功能:
def locate(filename,s):
lookfor=s
new_list=[]
with open(filename) as my_file:
for num, line in enumerate(my_file):
if lookfor in line:
new_list.append(num)
new_list.sort()
return new_list
答案 0 :(得分:0)
def locate(filename,s):
lookfor=s
new_list=[]
with open(filename) as my_file:
for num, line in enumerate(my_file,1):
if lookfor in line:
new_list.append(num)
new_list.sort()
return new_list