如何从不同位置返回文本文件中的数字列表

时间:2013-09-13 10:51:01

标签: python text-files

我正在尝试使用python从结果文本文件中导入数字列表。问题是列表的位置取决于所需数据的大小。我对我感兴趣的结果文件的部分看起来像......

Tip Rotation (degrees)
Node    , UR[x]     , UR[y]     , UR[z]
101     , 0.7978    , 0.7955    , -2.6071
102     , -0.7978   , -0.7955   , -2.6071
1303    , 0.7963    , 0.7693    , -2.6053
1304    , 0.7944    , 0.7150    , -2.5948
1305    , 0.7797    , 0.6616    , -2.5697
1306    , 0.7427    , 0.6074    , -2.5279
1307    , 0.6893    , 0.5509    , -2.4701
1308    , 0.6214    , 0.4914    , -2.3998
1309    , 0.5421    , 0.4272    , -2.3227
1310    , 0.4538    , 0.3585    , -2.2451
1311    , 0.3589    , 0.2848    , -2.1736
1312    , 0.2594    , 0.2070    , -2.1141
1313    , 0.1568    , 0.1256    , -2.0715

所以基本上我想在UR [z]列中列出一个值,但是这个块的位置会因节点数量等而有所不同。所以我在考虑搜索UR [z]然后返回其列中的值。另外,我想在没有指定节点数的情况下这样做,但如果能够大大简化节点,就可以做到。

任何帮助将不胜感激!干杯!

进一步说明结果文件的其余部分如下!

SUMMARY OF RESULTS
Max tip rotation =,-2.6071,degrees
Min tip rotation =,-2.0493,degrees
Mean tip rotation =,-2.3655,degrees
Max tip displacement =,2.4013,mm
Min tip displacement =,1.0670,mm
Mean tip displacement = ,1.6051,mm
Max Tsai-Wu FC =,0.3904
Max Tsai-Hill FC =,0.3909

PLATE MODEL DATA
Length =,1000.00,mm
Width =,500.00,mm
Pretwist =, 65.00,degrees
Number of mesh elements =,  1250
Number of nodes =,  1326

VAT FIBRE PATH DEFINITION
Fibre path formula =
            2
-6.096e-17 x + 1.421e-15 x - 90
Number of partitions = ,    50
Partition No    , Ply Angle (degrees)
     1      ,-90.00
    25      ,-90.00
    50      ,-90.00

VAT Ply Orientations in each region
Region  , Angle (degrees)
1       ,-90.0
2       ,-90.0
3       ,-90.0
4       ,-90.0
5       ,-90.0
6       ,-90.0
7       ,-90.0
8       ,-90.0
9       ,-90.0
10      ,-90.0
11      ,-90.0
12      ,-90.0
13      ,-90.0
14      ,-90.0
15      ,-90.0
16      ,-90.0
17      ,-90.0
18      ,-90.0
19      ,-90.0
20      ,-90.0
21      ,-90.0
22      ,-90.0
23      ,-90.0
24      ,-90.0
25      ,-90.0
26      ,-90.0
27      ,-90.0
28      ,-90.0
29      ,-90.0
30      ,-90.0
31      ,-90.0
32      ,-90.0
33      ,-90.0
34      ,-90.0
35      ,-90.0
36      ,-90.0
37      ,-90.0
38      ,-90.0
39      ,-90.0
40      ,-90.0
41      ,-90.0
42      ,-90.0
43      ,-90.0
44      ,-90.0
45      ,-90.0
46      ,-90.0
47      ,-90.0
48      ,-90.0
49      ,-90.0
50      ,-90.0

ADDITIONAL DATA

Tip Rotation (degrees)
Node    , UR[x] , UR[y] , UR[z]
101 , 0.7978    , 0.7955    , -2.6071
102 , -0.7978   , -0.7955   , -2.6071
1303    , 0.7963    , 0.7693    , -2.6053
1304    , 0.7944    , 0.7150    , -2.5948
1305    , 0.7797    , 0.6616    , -2.5697
1306    , 0.7427    , 0.6074    , -2.5279
1307    , 0.6893    , 0.5509    , -2.4701
1308    , 0.6214    , 0.4914    , -2.3998
1309    , 0.5421    , 0.4272    , -2.3227
1310    , 0.4538    , 0.3585    , -2.2451
1311    , 0.3589    , 0.2848    , -2.1736
1312    , 0.2594    , 0.2070    , -2.1141
1313    , 0.1568    , 0.1256    , -2.0715
1314    , 0.0525    , 0.0421    , -2.0493
1315    , -0.0525   , -0.0421   , -2.0493
1316    , -0.1568   , -0.1256   , -2.0715
1317    , -0.2594   , -0.2070   , -2.1141
1318    , -0.3589   , -0.2848   , -2.1736
1319    , -0.4538   , -0.3585   , -2.2451
1320    , -0.5421   , -0.4272   , -2.3227
1321    , -0.6214   , -0.4914   , -2.3998
1322    , -0.6893   , -0.5509   , -2.4701
1323    , -0.7427   , -0.6074   , -2.5279
1324    , -0.7797   , -0.6616   , -2.5697
1325    , -0.7944   , -0.7150   , -2.5948
1326    , -0.7963   , -0.7693   , -2.6053

Tip Displacement (mm)
Node    , U[x]  , U[y]  , U[z]
101 , 9.2757    , -4.6016   , 2.4013
102 , -9.2757   , 4.6016    , 2.4013
1303    , 8.4491    , -4.2173   , 2.2646
1304    , 7.6214    , -3.8331   , 2.1175
1305    , 6.8005    , -3.4481   , 1.9597
1306    , 5.9917    , -3.0625   , 1.8024
1307    , 5.2006    , -2.6792   , 1.6515
1308    , 4.4320    , -2.3003   , 1.5123
1309    , 3.6888    , -1.9278   , 1.3887
1310    , 2.9721    , -1.5627   , 1.2830
1311    , 2.2807    , -1.2054   , 1.1973
1312    , 1.6116    , -0.8552   , 1.1323
1313    , 0.9596    , -0.5107   , 1.0888
1314    , 0.3186    , -0.1698   , 1.0670
1315    , -0.3186   , 0.1698    , 1.0670
1316    , -0.9596   , 0.5107    , 1.0888
1317    , -1.6116   , 0.8552    , 1.1323
1318    , -2.2807   , 1.2054    , 1.1973
1319    , -2.9721   , 1.5627    , 1.2830
1320    , -3.6888   , 1.9278    , 1.3887
1321    , -4.4320   , 2.3003    , 1.5123
1322    , -5.2006   , 2.6792    , 1.6515
1323    , -5.9917   , 3.0625    , 1.8024
1324    , -6.8005   , 3.4481    , 1.9597
1325    , -7.6214   , 3.8331    , 2.1175
1326    , -8.4491   , 4.2173    , 2.2646

END OF RESULTS

我只想查看提示旋转数据部分!

3 个答案:

答案 0 :(得分:2)

不使用任何模块:

with open('filename') as f:
    next(f)             #skip first line
    nodes = map(str.strip, next(f).split(','))
    #find the index of `'UR[z]'` in `nodes` list
    column = nodes.index('UR[z]')
    #iterate over each line one by one
    for line in f:
        line = map(str.strip, line.split(','))
        #return the desired column
        print line[column]

<强>输出:

-2.6071
-2.6071
-2.6053
-2.5948
-2.5697
-2.5279
-2.4701
-2.3998
-2.3227
-2.2451
-2.1736
-2.1141
-2.0715

<强>更新

with open('filename') as f:
    column = None
    for line in f:
        if 'UR[z]' in line:
            line = map(str.strip, line.split(','))
            column = line.index('UR[z]')
            break
    if column is not None:
        for line in f:
            if not line.strip():
                break
            print map(str.strip, line.split(','))[column]

<强>输出:

-2.6071
-2.6071
-2.6053
-2.5948
-2.5697
-2.5279
-2.4701
-2.3998
...
-2.5948
-2.6053

答案 1 :(得分:1)

你可以做类似这样的事情(虽然浮点解析很粗糙):

import csv

with open('data.txt','r') as fh:
    reader = csv.reader(fh)
    for row in reader:
        try:
            print float(row[3])
        except: pass

答案 2 :(得分:1)

您可以使用numpy genfromtxt

import numpy as np
data = np.genfromtxt('yourfile.txt',delimiter=' , ',skip_header=1, names=True)

然后像这样访问你的专栏

data['URx']