如何处理一个大的文本文件以更有效地获取这个文件的一部分?

2024-04-18 21:30:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个文本文件:

  A B C D E F ... X Y Z
a 1 0 1 2 1 0 ... 1 0 2
b 1 2 0 1 1 2 ... 1 0 0
c . . . . . . ..... . .
d . . . . . . ..... . .
e . . . . . . ..... . .
f . . . . . . ..... . .
. . . . . . . ..... . .
. . . . . . . ..... . .
. . . . . . . ..... . .
x 1 0 1 2 1 0 ... 1 0 2
y 0 0 1 0 1 1 ... 1 0 2
z 1 2 0 1 1 2 ... 1 0 0

我需要做的是:加载这个文件并将1000行E&F行放到一个新的文本文件中 我曾使用itertools加载这个大文件,但无法提高E&F行的效率

#!/usr/bin/env python
# -*- coding:UTF-8 -*-
from itertools import islice
fout = open('a.txt','w')
with open('b.txt','r') as fin:
    n = 50
    while n > 0:
    next_n_lines = list(islice(fin,0,20))
    if not next_n_lines:
        break
    fout.write(''.join(next_n_lines))
    n = n - 1
fin.close()
fout.close()

Tags: 文件envtxtclosebinusropennext
2条回答

因为列由“”分隔,所以可以使用str.split。你知道吗

def extract(nin,nout,cidx,rows):
    with open(nout,'w') as fout:        
        with open(nin,'r') as fin:
            offest = 0
            for line in fin:
                 cols = line.strip().split(' ')
                 for cc in cidx:
                     fout.write(cols[cc+offest] )
                     fout.write(' ')
                 fout.write('\n')
                 offest = 1
                 rows -= 1
                 if rows == 0:
                     break                     
extract('b.txt','a.txt',[4,5],1000)

你可以使用这个代码

with open('a.txt','w') as fout:
    with open('b.txt','r') as fin:
        lines_done=0
        fin.readline() # skip the first line ("  A B C D E F ... X Y Z" in your example)
        # fout.write("E F\n") # uncomment this line if you want the column headings in fout
        for line in fin:  
            if lines_done>=1000: # you said 100 lines only
                break     
            ef=line[10:13] # solution A
            # ef=" ".join(line.split()[5:7]) # solution B
            fout.write(ef+"\n")
            lines_done+=1

请确定您需要哪种解决方案

  • A适用于示例数据(E为10,F为12),速度更快
  • B适用于更一般的空格分隔行(E第5行,F第5行) 第6个条目)并且有点慢

相关问题 更多 >