import re
sum=0
file = open("pro.txt").readlines()
for lines in file:
word= len(re.findall('(^|[^\w\-])able#1(?=([^\w\-]|$))', lines))
if word>0:
sum=sum+1
pro.txt如何使用python從文本文件中檢索值?
0 6 9 able#1
0 11 34 unable#1
9 12 22 able#1
0 6 9 able#1-able#1
0 11 34 unable#1*able#1
我想這個詞的價值,就像如果用戶輸入的句子,它包含單詞能比它取回針對它的價值就像9 6 0
或0 6 9
,但現在作爲一個樣本,我只是想,如果我在這個txt文件只專注於能#1字我怎樣才能通過它檢索值,因爲我只是想它在某種程度上分裂,並不僅僅是把隊列上
for lines in file:
k=lines.split()
print k
['0', '6', '9', 'able#1', 'you#1']
['0', '11', '34', 'unable#1']
['9', '12', '22', 'able#1']
['0', '6', '9', 'able#1-able#1']
['0', '11', '34', 'unable#1*able#1']
['9', '12', '22', 'able#1_able#1']
預計產量:
enter the word you want to find in text file : able#1
word found !!
values are
0 6 9
什麼是您預期的輸出? – ATOzTOA 2013-02-26 18:06:25