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

标签 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] 列中列出值,但是这个 block 的位置根据节点的数量等而有所不同。所以我想说搜索 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

我只想看尖端旋转数据部分!

最佳答案

不使用任何模块:

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

关于python - 如何从不同位置的文本文件返回数字列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18784518/

相关文章:

c++ - 处理巨大的文本文件

Python 多个用户同时追加到同一个文件

python - 获取模型字段作为字符串

python - 关于 GUI 计时器的建议,以显示后台线程的运行时间?

python - 尝试使用 FastAPI 和 python-docx 库读取 docx 文件 : AttributeError: 'bytes' object has no attribute 'seek' error

javascript - 如何迭代 "line-by-line"npm 中的所有行?

java - 我可以输出一个没有过多空格的数组吗?

python - 了解 scikit CountVectorizer 中的 min_df 和 max_df

python - Discord.py 重启命令