我需要读取一些非常大的文本文件(100+ Mb),使用正则表达式处理每一行,并将数据存储到一个结构中。我的结构继承自defaultdict,它有一个read(self)方法来读取self.file_name文件。
看看这个非常简单(但不是真实的)例子,我没有使用正则表达式,而是分割了行:
看看这个非常简单(但不是真实的)例子,我没有使用正则表达式,而是分割了行:
import multiprocessing
from collections import defaultdict
def SingleContainer():
return list()
class Container(defaultdict):
"""
this class store odd line in self["odd"] and even line in self["even"].
It is stupid, but it's only an example. In the real case the class
has additional methods that do computation on readen data.
"""
def __init__(self,file_name):
if type(file_name) != str:
raise AttributeError, "%s is not a string" % file_name
defaultdict.__init__(self,SingleContainer)
self.file_name = file_name
self.readen_lines = 0
def read(self):
f = open(self.file_name)
print "start reading file %s" % self.file_name
for line in f:
self.readen_lines += 1
values = line.split()
key = {0: "even", 1: "odd"}[self.readen_lines %2]
self[key].append(values)
print "readen %d lines from file %s" % (self.readen_lines, self.file_name)
def do(file_name):
container = Container(file_name)
container.read()
return container.items()
if __name__ == "__main__":
file_names = ["r1_200909.log", "r1_200910.log"]
pool = multiprocessing.Pool(len(file_names))
result = pool.map(do,file_names)
pool.close()
pool.join()
print "Finish"
最后,我需要将每个结果合并到一个容器中。保留行的顺序非常重要。我的方法在返回值时速度太慢。有更好的解决方案吗?我使用的是Linux上的Python 2.6。