我有几个文本,我想知道行号和出现单词的文件。
我得到了很好的文件,但没有得到行号。
这是地图
#!/usr/bin/env python
import sys
import os
find = 'but' #word to find
linesCont = 0
file = os.environ["map_input_file"]
for line in sys.stdin:
line = line.strip()
words = line.split()
linesCont = linesCont + 1;
for word in words:
if (word in (find)):
print '%s\t%s' % (file, linesCont)
这是减少
#!/usr/bin/env python
import sys
result = {}
for line in sys.stdin:
line = line.strip()
file, lineNumber = line.split('\t', 1)
try:
result[file] = result[file] + ', ' + lineNumber
except:
result[file] = 'File "%s". LineNumber(s): %s' % (file, lineNumber)
for file in result.keys():
print '%s\t' % (result[file])
非常感谢提前