2016-03-24 34 views
0

Belirli bir satırı (10884121) 30 milyon satır uzunluğunda bir metin dosyasında silmeye çalışıyorum. Bu, ilk denediğim yöntemdir, ancak çalıştırdığımda yaklaşık 20 saniye çalışır ve bana bir "bellek hatası" verir. Bunu yapmanın daha iyi bir yolu var mı? Teşekkürler!Python Belirli bir Satır numarasını sil

import fileinput 
import sys 

f_in = 'C:\\Users\\Lucas\\Documents\\Python\\Pagelinks\\fullyCleaned2.txt' 
f_out = 'C:\\Users\\Lucas\\Documents\\Python\\Pagelinks\\fullyCleaned3.txt' 

with open(f_in, 'r') as fin: 
    with open(f_out, 'w') as fout: 
     linenums = [10884121] 
     s = [y for x, y in enumerate(fin) if x not in [line - 1 for line in linenums]] 
     fin.seek(0) 
     fin.write(''.join(s)) 
     fin.truncate(fin.tell()) 
+1

(. '' (Ler) katılmak) 'numaralandırmak (yüzgeç)' ve 'fin.write ile belleğe dosyanın tamamını okumak yardım için' –

cevap

1

Her şeyden önce, ithalatı kullanmıyordunuz; Giriş dosyasına yazmaya çalışıyorsunuz ve kodunuz tüm dosyayı belleğe okuyor.

Böyle bir şey daha az güçlükle işe yarayabilir - satır numaralarını saymak için enumerate kullanım satırını okuyoruz;

f_in = 'C:\\Users\\Lucas\\Documents\\Python\\Pagelinks\\fullyCleaned2.txt' 
f_out = 'C:\\Users\\Lucas\\Documents\\Python\\Pagelinks\\fullyCleaned3.txt' 

ignored_lines = [10884121] 
with open(f_in, 'r') as fin, open(f_out, 'w') as fout: 
    for lineno, line in enumerate(fin, 1): 
     if lineno not in ignored_lines: 
      fout.write(line) 
+0

teşekkürler etmeyin! Ben python için yeniyim, bu yüzden hala her şeyin nasıl çalıştığını öğreniyorum. – lsch91

0

kullanmayı deneyin: bir numaralı görmezden hatlarının listesi yılında değilse ve her hat için biz çıkışa yazmak

biterse yüksek ihtimalle
import fileinput 

f_in = 'C:\\Users\\Lucas\\Documents\\Python\\Pagelinks\\fullyCleaned2.txt' 
f_out = 'C:\\Users\\Lucas\\Documents\\Python\\Pagelinks\\fullyCleaned3.txt' 

f = open(f_out,'w') 

counter=0 

for line in fileinput.input([f_in]): 
    counter=counter+1 
    if counter != 10884121: 
      f.write(line) # python will convert \n to os.linesep, maybe you need to add a os.linesep, check 

f.close() # you can omit in most cases as the destructor will call it 
0

Dosyayı listeye kaydetmeye çalıştığınız için bellek. Aşağıda bu deneyin :

import fileinput 
import sys 

f_in = 'C:\\Users\\Lucas\\Documents\\Python\\Pagelinks\\fullyCleaned2.txt' 
f_out = 'C:\\Users\\Lucas\\Documents\\Python\\Pagelinks\\fullyCleaned3.txt' 
_fileOne = open(f_in,'r') 
_fileTwo = open(f_out,'w') 
linenums = set([10884121]) 
for lineNumber, line in enumerate(_fileOne): 
    if lineNumber not in linenums: 
     _fileTwo.writeLine(line) 
_fileOne.close() 
_fileTwo.close() 

Burada dosyayı satır satır okuyup gerekli olmayan satırları kapsam dışında tutmak, bu bellek yetersiz çalışmayabilir. Ayrıca, arabellek kullanarak dosyayı okumayı deneyebilirsiniz. Bu yardımcı olur umarım.

0

Genel dosya filtresi işlevi nedir?

def file_filter(file_path, condition=None): 
    """Yield lines from a file if condition(n, line) is true. 
     The condition parameter is a callback that receives two 
     parameters: the line number (first line is 1) and the 
     line content.""" 

    if condition is None: 
     condition = lambda n, line: True 

    with open(file_path) as source: 
     for n, line in enumerate(source): 
      if condition(n + 1, line): 
       yield line 

open(f_out, 'w') as destination: 
    condition = lambda n, line: n != 10884121 

    for line in file_filter(f_in, condition): 
     destination.write(line) 
İlgili konular