English 中文(简体)
甲型六氯环己烷:高效文档
原标题:Python: efficient file io

在两个大档案中同时阅读的最有效(测试)方式是什么,有些处理方法如何?

我有两处档案;一、一、二、一、二、一、二、一、二、一、二、一、二、一、二、一、二、一、二、一、二、三、一、一、二、一、二、一、二、三、一、四、一、一、一、四、一、一、一、二、三、一、四、一、一、一、一、一、四、一、一、一、二、一、一、一、二、一、一、一、二、一、一、一、二、一、一、一、一、二。 我的目标是在两个档案中阅读,然后在每条楼面进行一些处理。

def kernel:
    a_file=open( a.txt , r )
    b_file=open( b.txt ,  r )
    a_line = a_file.readline()
    b_line = b_file.readline()
    while a_line:
        process(a_spl,b_spl) #process requiring both corresponding file lines

我看着透视线和阅读线,但想一看一看是否做得更好。 速度对这项任务至关重要。

谢谢。

最佳回答

除非<代码>process功能本身如此,否则以下代码不积累存储输入文档中的数据。

from itertools import izip

def process(line1, line2):
  # process a line from each input

with open(file1,  r ) as f1:
  with open(file2,  r ) as f2:
    for a, b in izip(f1, f2):
      process(a, b)

如果<编码>程序>功能有效,则该代码的运行速度应足以达到多数目的。 <<>>>>t>/code> loop在文档到达时终止。 如果这两种档案都含有一条超长线(即XML、JSON),或者如果档案不是案文,则该法典可能不会奏效。

问题回答

您可使用<条码>,说明,以确保您的档案在处决后结案。 http://effbot.org/zone/python-with-statement.htm“rel=“nofollow” blog entry:

公开档案,处理其内容,确保结案,你可以做:

with open("x.txt") as f:
    data = f.read()
    do something with data

伊斯兰团结组织的力量可能非常快——也许你的处理会减缓事情。 a. 考虑一个简单的投入循环,以便提出这样一个问题:

queue = multiprocessing.Queue(100)
a_file = open( a.txt )
b_file = open( b.txt )
for pair in itertools.izip(a_file, b_file):
     queue.put(pair) # blocks here on full queue

您可以建立一套程序,将项目从点上拉出来,并采取行动,假定你的问题可以这样并行。

d 我将你的情况改为以下条件,这样一来,如果比b条更有条理,就会失败。

while a_line and b_line

Otherwise, that looks good. You are reading in the two lines that you need, then processing. You could even multithread this by reading in N pairs of line and sending each pair off to a new thread or similar.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...