使用ssh从100台计算机对文件进行Python字数计算

使用ssh从100台计算机对文件进行Python字数计算,python,multithreading,ssh,paramiko,Python,Multithreading,Ssh,Paramiko,我试图为give文件编写一个字数计算代码。当我运行这个程序时,我的字典中有一个空的,我只是想得到单词及其频率。我不确定这哪里错了 import collections, re class Wordcount(object): def __init__(self): self.freq_dict = collections.defaultdict(int) def count(self,input_file): with open(input_f

我试图为give文件编写一个字数计算代码。当我运行这个程序时,我的字典中有一个空的,我只是想得到单词及其频率。我不确定这哪里错了

import collections, re

class Wordcount(object):
    def __init__(self):
        self.freq_dict = collections.defaultdict(int)

    def count(self,input_file):
        with open(input_file) as f:
            for line in f:
                words = line.rstrip().strip().split()
                for word in words:
                    word = word.lower()
                    word = re.sub("[^A-Za-z0-9]+",'',word)
                    self.freq_dict[word]+=1
        print self.freq_dict

def Main():
    c1 = Wordcount()
    c1.count('out.txt')
我的out.txt是这样的

The quick brown fox jumps over the lazy dog

--
 asd
 asdasd


The quick brown fox jumps over the lazy dog's

The quick brown fox jumps over the lazy dog
asd to之前的空格被解析到字典中

defaultdict(<type 'int'>, {'': 1, 'brown': 3, 'lazy': 3, 'over': 3, 'fox': 3, 'dog': 2, 'asdasd': 1, 'dogs': 1, 'asd': 1, 'quick': 3, 'the': 6, 'jumps': 3})
defaultdict(,{':1,'brown':3,'lazy':3,'over':3,'fox':3,'dog':2,'asdasd':1,'dogs':1,'asd':1,'quick':3,'the':6,'jumps':3})
我还想将ssh的这一部分扩展到近1000台机器,读取文件并增加单词的频率。最好的方法是什么?我应该创建一个线程T1,用于登录计算机并将登录信息传递给另一个线程以读取文件,然后再传递给另一个线程以单独递增散列值


任何关于如何扩展的建议都非常有用吗?

这里是一个简单的例子。Fabric是一个允许通过ssh在多台机器上执行命令的框架

from fabric.api import task, run, get
from collections import Counter
from StringIO import StringIO


def worlds(data):
    return data.split()


@task
def count_worlds():
    s_fp = StringIO()
    # for big files better read to temp file
    get('/some/remote/file', s_fp)
    world_count = Counter(s_fp.getvalue())
    # do something with world_count
要在许多机器上执行此脚本,只需将其保存到fabfile.py并执行:

$ fab count_worlds -H host1,host2,host3
您还可以在fabfile中定义主机,有关更多信息,请参阅。
当然,您应该首先安装fabric。

Map/Reduce技术?是的,这是MR工作,但我只想使用Python!!避免空行的提示:检查行是否为空。只要剥开就足够了,去掉rstrip。更好地用于计数。用于拆分创建迭代器。谢谢!这绝对是我想要的。