有条件地导入文本文件中的行

时间:2016-03-15 02:09:58

标签: python numpy scipy

我在这里有一个代码,我在其中导入文本文件的内容并进行一些绘图。我的要求是,我想通过给出条件只导入那些数据行。在对导入的数据进行一些计算之后,我想导入文本文件中剩余的其余行并进行进一步的计算。完成此过程后,我想将两个计算分别合并为两个数组。

这是我的示例代码。我还将文本文件上传到了dropbox。

import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate  import RectBivariateSpline
import numdifftools as nd
plt.ioff()

data5= np.loadtxt('textfile',skiprows=1,unpack = True).T
ts3 = data5[:,1] ## Condition based on this data
fillend= 37900   ## Read rows only till this value
ft3 = data5[:,0]
t3 = data5[:,2]
Ek3 = data5[:,13]
ek3 = data5[:,14]
eko3 = data5[:,15]
Eko3 = data5[:,16]

## Initially I want to read only all the rows until ts3=37900 with the row corresponding to fillend also included
## Until ts3 =37900, this is the calculation that I want to make

e2 = interpolate.InterpolatedUnivariateSpline(t2,Ek2,k=5) ## Here length of Ek2 and t2 should be equal to 378)

df2 =nd.Derivative(e2,method='central',n=1,order=2,full_output=False)
e2 = (1-df2(t2))
df2 = df2(t2)

## Beyond ts3 = 37900, i.e from 38000 onwards, this is the calculation that I want to make
df2 =nd.Derivative(e2,method='central',n=1,order=2,full_output=False)
e2 = (-df2(t2))
df2 = df2(t2)

## At the end of the operation, I want to have only two arrays e2 and df2 with all the values calculated

Sample file

1 个答案:

答案 0 :(得分:1)

这是一个基于行中的值在两个块中加载txt(行)的简单示例。我正在模拟一个包含字符串列表的文件。 foo是一个生成器函数,它以2个块的形式返回输入。

import numpy as np

t0 = '1, 2, 3, 4'
t1 = '4, 5, 6, 7'
txt=[t0,t0,t0,t1,t1,t1]

def foo(txt):
    lines = []
    while txt:
        l = txt.pop(0)
        if int(l.split(',')[1])<4: lines.append(l)
        else:
            yield lines
            break
    yield [l]+txt

for lines in foo(txt[:]):
    print(np.loadtxt(lines, delimiter=','))

结果是2个数组:

2033:~/mypy$ python stack36001473.py 
[[ 1.  2.  3.  4.]
 [ 1.  2.  3.  4.]
 [ 1.  2.  3.  4.]]
[[ 4.  5.  6.  7.]
 [ 4.  5.  6.  7.]
 [ 4.  5.  6.  7.]]