<p>最后得到了提取文本的工作脚本</p>
<p>#从文本文件中读取特定行
#例如,在程序中,我们读取文件并只打印标题2和标题4下的行
#日志文件可能包含空行
#示例日志文件
#职务
# 1
#dklfjsdkl公司;
#克
#sdfzsdfsdf公司
#sdfsdfsdf公司
#dsfsdfsd公司
#dfsdf公司
#
#职务
# 2
#
#dfdf公司
#dfdf公司
#dfdf公司
#测向
#dfd公司
#d级
#
#标题3
#sdfdfd公司
#DFD公司
#dfd公司
#
#dfd公司
#
#职务
# 4
#dfkdfkd公司
#dfdkjmd公司
#dfdkljm公司</p>
<pre><code>in_list= []
while True:
i = raw_input("Enter title to be extracted (or Enter to quit): ")
in_list.append(i)
if not i:
break
print("Your input:", i)
print("While loop has exited")
in_list.remove(i)
print "Input list", in_list
flist = []
with open("C:\\text.txt", 'r') as inp:
#read the flie and storing into the list
flist =inp.readlines()
inp.close()
#making everything in the list to lower case
flist = map(lambda x:x.lower(),flist)
flist = [s.strip("\n") for s in flist]
print flist
# printing the complete log file from the list. Since once we put the vlaue in the list the new line character will be \ appended in the list element.
#hence striping with \n character
# for i in flist:
# print i.strip("\\n")
for j in range(len(in_list)):
result = any(in_list[j] in word for word in flist)
if result:
i_index = flist.index(in_list[j])
flag = 0
with open("C:\\output.txt",'a') as f1:
f1.write(flist[i_index])
f1.write("\n")
while flag ==0:
if "title" in flist[i_index+1]:
flag =1
else:
i_index += 1
f1.write(flist[i_index])
f1.write("\n")
i_index += 1
f1.close()
</code></pre>