python - fileinput - indexing lines out of order -
i have got huge txt file. cannot use readlines() read because memory error appeared, have started use fileinput. works until need write of lines file, got "accessing lines out of order". here part of script:
input_4=fileinput.input([plik0_a]) out=open('out_file','w') in range(s,e): out.writelines('%s' % input[i])
please me find way write lines, number == i. assume not difficult beginner:).
i need work that(part of script below) huge file.
n=10918 s=(int(start)-n) e=(int(end)-n+1) czyta_4=open(plik0_a,'r') zczyta_4=czyta_4.readlines() in range(s,e): out.writelines('%s' % +': '+ '%s' % zczyta_4[i])
the result (and want huge file, presented below):
0: fixedstep chrom=chr1 start=10918 step=1 1: 0.064 2: 0.058 3: 0.064 4: 0.058 5: 0.064 6: 0.064 7: 0.064 8: 0.064 9: 0.064 10: 0.058 . . . s : 0.058
you don't need use fileinput, file object iterable
import itertools open(plik0_a) input_4, open('out_file','w') out: out.writelines(itertools.islice(input_4, s, e))
note file pointing after line e
, you'll have subtract if want islice more lines.
eg
import itertools open(plik0_a) input_4, open('out_file','w') out: out.writelines(itertools.islice(input_4, 10, 20)) # lines 11-20 out.writelines(itertools.islice(input_4, 10, 20)) # lines 31-40
Comments
Post a Comment