使用python从文本文件中提取数据列

时间:2017-04-06 22:51:08

标签: abaqus

我想从文本文件中提取一列数据。

我的文本文件如下:( TOTAL TIME / FREQ是我要提取下面数字的列!)

Abaqus/Standard 6.14-1                  DATE 07-Apr-2017 TIME 02:58:26
 SUMMARY OF JOB INFORMATION:
 STEP  INC ATT SEVERE EQUIL TOTAL  TOTAL      STEP       INC OF       DOF    IF
               DISCON ITERS ITERS  TIME/    TIME/LPF    TIME/LPF    MONITOR RIKS
               ITERS               FREQ
   1     1   1     3    10    13  0.0100     0.0100     0.01000   
   1     2   1U    1     4     5  0.0100     0.0100     0.01000   
   1     2   2     2     3     5  0.0125     0.0125     0.002500  
   1     3   1     1     4     5  0.0150     0.0150     0.002500  
   1     4   1     2     4     6  0.0188     0.0188     0.003750  
   1     5   1     2     5     7  0.0244     0.0244     0.005625  
   1     6   1     2     3     5  0.0300     0.0300     0.005625

你能告诉我如何在python中编写这段代码,可以提取数字然后得到如下数字列表吗?

[0.0100,0.0100,0.0125,0.0150,0.0188,0.0244,0.0300]

1 个答案:

答案 0 :(得分:1)

这个脚本应该有所帮助。我已经评论说明了每一行有助于您为要创建的其他脚本调整此值。

# A function to check if a value is an integer
def RepresentsInt(s):
    try: 
        int(s)
        return True
    except ValueError:
        return False


#create empty output list
StepTimes = []

#open the text file
file1 = open('datFile.dat','r')

#read the text file line by line
for line in file1:

    #split file into a list
    line = line.split()
    #check if first item in list is an integer
    if RepresentsInt(line[0]):
        #append step time to a new list
        StepTimes.append(line[6])

print (StepTimes)

返回

['0.0100', '0.0100', '0.0125', '0.0150', '0.0188', '0.0244', '0.0300']