我有一個代碼,我在這裏導入文本文件的內容並做一些繪圖。我的要求是,我想通過給出條件只導入那些行數據。在對導入的數據進行一些計算之後,我想要導入文本文件中剩餘的其餘行並進行進一步計算。這個過程完成後,我想分別將兩個計算組合成兩個數組。有條件地導入文本文件中的行
這是我有的示例代碼。我也上傳了文本文件到保管箱中。
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
我也說不上來,一目瞭然,在此代碼什麼是工作,什麼是公正需要,和/或是否有錯誤。如果這是行得通的,那麼問題是什麼? – hpaulj
@hpaulj我的目標是導入所有行,直到ts3 = 37900包括。然後我想要做一些計算,如上所示。計算完成後,我想導入其餘的行並進行第二次計算。在代碼中,這沒有實現。它只是要導入所有的行並進行計算。 –
'loadtxt'逐行讀取文件,將結果收集到列表中。您可以執行相同的讀取。 'loadtxt'接受來自任何代碼的輸入。因此,您的文件讀取器可以逐行讀取文件,並將它們傳遞給'loadtxt',或退出。 – hpaulj