仅针对某些行读取文本文件以在Python

时间:2015-07-01 20:51:49

标签: python numpy text-files

我正在使用np.loadtxt加载一个文本文件,并希望将python拆分为四个。 通常我只是将每组数据粘贴到不同的文本文件中,并为每个文本文件执行np.loadtxt,但是我将不得不这样做数百次,因此太费时间了。

这是文本文件的缩短版本。 所以我要做的是让python读取第一个数字(0.6999)并丢弃它,然后读取下面5行的值并为每列分配变量名,然后接下来的5行包含变量to每一列,等等。

  

有什么方法可以告诉python可能只对第1行执行np.loadtext,然后只对第2行到第6行,然后是7到12等?

   0.699999988
   1    0.2000    0.0618
   2    0.2500    0.0417
   3    0.3000    0.0371
   4    0.3500    0.0390
   5    0.4500    0.0761
    670.0000  169.4000 6.708E-09
    635.0001  169.1806 1.584E-08
    612.9515  168.6255 2.724E-08
    591.2781  168.2719 4.647E-08
  670.00  0.0E+00  0.0E+00  0.0E+00  0.0E+00  0.0E+00  0.0E+00  0.0E+00
  635.00  9.8E-07  4.2E-07  2.1E-07  1.2E-07  4.4E-08  1.8E-08  1.4E-08
  612.95  6.0E-06  3.5E-06  2.1E-06  1.3E-06  4.7E-07  1.8E-07  1.4E-07
  591.28  2.2E-05  1.3E-05  7.7E-06  4.9E-06  1.8E-06  6.6E-07  5.0E-07
  569.98  8.3E-05  5.0E-05  2.8E-05  1.8E-05  6.4E-06  2.4E-06  1.8E-06
  549.06  3.0E-04  1.8E-04  1.0E-04  6.2E-05  2.3E-05  8.4E-06  6.4E-06
  528.51  7.8E-04  5.0E-04  2.8E-04  1.7E-04  6.2E-05  2.3E-05  1.8E-05
  508.34  1.6E-03  1.0E-03  5.8E-04  3.4E-04  1.3E-04  4.9E-05  3.7E-05

以下是我用于三种不同文本文件的内容:

altvall,T,Pp= np.loadtxt('file1.txt',usecols = (0,1,2),unpack=True) # load text file

tau1,tau2,tau3,tau4,tau5,tau6,tau7 = np.loadtxt('file2.txt',usecols = (1,2,3,4,5,6,7),unpack=True) # load text file

wvln,alb = np.loadtxt('file3.txt',usecols = (1,2),unpack=True) # load text file

现在我只想要类似的东西,但不将我的文本文件分成不同的部分。

1 个答案:

答案 0 :(得分:1)

一种简单的方法是使用itertools.izip_longest将输入文件的行分组为5个。关键是要执行以下操作:

for rows in izip_longest(*[file_object]*N):
    # rows will be a tuple of N consecutive rows
    # do something with rows

完整示例:

import numpy as np
from itertools import izip_longest

data = []
with open(filehandle, 'r') as fin:
    fin.next() # skip first line
    for rows in izip_longest(*[fin]*5): # read fin 5 rows at a time
        rows = [map(float, r.strip().split()) for r in rows]
        data.append(np.array(rows))

这会产生一个5xN阵列列表:

>>> print data
[array([[ 1.    ,  0.2   ,  0.0618],
       [ 2.    ,  0.25  ,  0.0417],
       [ 3.    ,  0.3   ,  0.0371],
       [ 4.    ,  0.35  ,  0.039 ],
       [ 5.    ,  0.45  ,  0.0761]]),
 array([[  6.70000000e+02,   1.69400000e+02,   6.70800000e-09],
       [  6.35000100e+02,   1.69180600e+02,   1.58400000e-08],
       [  6.12951500e+02,   1.68625500e+02,   2.72400000e-08],
       [  5.91278100e+02,   1.68271900e+02,   4.64700000e-08],
       [  5.69980100e+02,   1.68055300e+02,   7.85900000e-08]]),
 array([[  6.70000000e+02,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00],
       [  6.35000000e+02,   9.80000000e-07,   4.20000000e-07,
          2.10000000e-07,   1.20000000e-07,   4.40000000e-08,
          1.80000000e-08,   1.40000000e-08],
       [  6.12950000e+02,   6.00000000e-06,   3.50000000e-06,
          2.10000000e-06,   1.30000000e-06,   4.70000000e-07,
          1.80000000e-07,   1.40000000e-07],
       [  5.91280000e+02,   2.20000000e-05,   1.30000000e-05,
          7.70000000e-06,   4.90000000e-06,   1.80000000e-06,
          6.60000000e-07,   5.00000000e-07],
       [  5.69980000e+02,   8.30000000e-05,   5.00000000e-05,
          2.80000000e-05,   1.80000000e-05,   6.40000000e-06,
          2.40000000e-06,   1.80000000e-06]])]