2016-04-27 47 views
0

我想使用Heapq合併來合併一堆充滿排序整數的臨時文件並寫入輸出文件。函數中的生成器正在返回值。 heapq.merge()看起來很好。該程序生成但沒有寫入TestWriteOutput.txt文件。我試過在打開文件的行後將測試寫入輸出文件(outf),但沒有寫入它。 TestWriteOutput.txt被創建,但是爲空。python heapq合併排序不能寫入輸出文件

import os 
import sys 
import array 
import tempfile 
import heapq 

cwd = os.getcwd() 
print "Current Directory: %s" % cwd 


#generator function to return one integer at a time from the tempfile  
to a list 

def numInFile(f): 
    while True: 
     #set input buffer to read 8KB 
     input_buffer = [int(x) for x in f.read(1024*2).split(',') if 
     x.strip().isdigit()] 
     #convert list of string values into integers 
     int_buffer = map(int, input_buffer) 
     f.seek(0) #reset flag 
     if not input_buffer: 
      break 
     for number in int_buffer: 
      yield number #returns one number at a time and appends to 
          iterator 



with open('age.txt', 'r+') as inf: 
    with open('TestWriteOutput.txt', 'w+') as outf: 
     outf.write('some test data') 
     outf.write('some more data') 
     #iterator for heapq merge 
     sorted_list =[] 
     while True: 
      a = [int(x) for x in inf.read(20000).split(',') if 
       x.strip().isdigit()] 
      int_a = map(int, a) 
      if not a: 
       break 
      f = tempfile.TemporaryFile() 
      #sort and write to temp file 
      outf_array = sorted(int_a) 
      #####print outf_array 
      f.write(str(outf_array)) 
      f.seek(0) 
      sorted_list.append(numInFile(f)) 



     write_to_file = array.array('i') 

     #heapq merge function merges multiple ordered lists into a 
      single list 
     for x in heapq.merge(*sorted_list): 
      out_buffer = 1024*4 
      write_to_file.append(x) 
      if len(write_to_file) >= out_buffer: 
       write_to_file.tofile(outf) 
       del write_to_file[:] 
     if write_to_file: 
      write_to_file.tofile(outf) 

回答

0

的問題是,在numInFile功能要重設文件指針回在每次迭代中的文件的開頭。這使得numInFile成爲取之不盡,用之不竭的發電機。

如果我改變numInFile到:

def numInFile(f): 
    while True: 
     #set input buffer to read 8KB 
     input_buffer = [int(x) for x in f.read(1024*2).split(',') if 
     x.strip().isdigit()] 
     #convert list of string values into integers 
     int_buffer = map(int, input_buffer) 
     if not input_buffer: 
      break 
     for number in int_buffer: 
      yield number #returns one number at a time and appends to iterator 

,並取下測試寫入到輸出文件,程序成功完成。

>>> import array 
>>> with open('TestWriteOutput.txt') as f: 
...  arr = array.array('i') 
...  arr.fromfile(f, 64) 
... 
>>> arr 
array('i', [3, 3, 4, 5, 6, 6, 8, 8, 8, 8, 10, 11, 12, 12, 13, 17, 21, 25, 29, 30, 36, 37, 38, 39, 40, 44, 44, 46, 50, 50, 50, 52, 53, 53, 55, 56, 57, 59, 62, 63, 63, 64, 64, 65, 65, 66, 67, 68, 69, 70, 72, 73, 73, 74, 75, 75, 75, 75, 75, 76, 76, 77, 78, 79]) 
+0

你是什麼意思通過刪除測試寫入輸出文件?你指的是「outf.write('一些測試數據')」嗎? –

+0

是的,這些線。 – snakecharmerb