gpt4 book ai didi

python - MapReduce Amazon Python 获取输入文件的行号

转载 作者:可可西里 更新时间:2023-11-01 14:54:26 26 4
gpt4 key购买 nike

我有几个文本,我想知道行号和出现单词的文件。

我得到了文件但没有行号。

这是 map

#!/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)

而且是reduce

#!/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])

提前致谢

最佳答案

Here是 Apache 论坛中关于相同内容的讨论。 Another在 SO 中查询。和 here是一个代码片段,用于获取正在处理的 block 的文件名。

关于python - MapReduce Amazon Python 获取输入文件的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19334056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com