2017-04-06 98 views
1

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

的文本文件,我有如下:(总时间/频率是我想的是下提取数列!)

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

此脚本应该有所帮助。我已经评论说明每行代码可以帮助您调整您希望创建的其他脚本的内容。

# 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'] 
+1

也可以做'StepTimes.append(浮法(线[6]))'如果你想要的数值了 – agentp

+0

@agentp好去处 – CodeCupboard