2016-04-27 18 views
0

Heapq Mergeを使用して、ソートされた整数でいっぱいの一時ファイルをマージし、出力ファイルに書き込もうとしています。関数内のジェネレータが値を返しています。 heapq.merge()はうまく見えます。プログラムはビルドされますが、TestWriteOutput.txtファイルには何も書き込まれません。私は、ファイルが開かれた行の後に出力ファイル(outf)にテスト書き込みを試みましたが、何も書き込まれませんでした。 TestWriteOutput.txtが作成されますが空白です。python heapq merge sortが出力ファイルに書き込めません

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

関連する問題