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

Python 如何从不同位置的文本文件返回数字列表,python,text-files,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.605

我正在尝试使用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

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

您可以这样做(不过对浮点解析有很大的影响):


您可以这样做(不过对浮点解析有很大的影响):


不使用任何模块:

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
更新:

-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
输出:

-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

不使用任何模块:

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
更新:

-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
输出:

-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
您可以使用:

然后像这样访问您的专栏

data['URx']
您可以使用:

然后像这样访问您的专栏

data['URx']


我没有完全理解你的问题。我想你们对UR[z]值的不同位置有问题吧?您是否考虑在CSV模块的帮助下解析文件?(看一看)我对python相当陌生,所以如果我没有把事情说清楚或者看起来很琐碎,请原谅。因此,我们基本上希望搜索UR[z],然后在其列中返回下面的(26)个值。目前结果文件是.txt格式的,.csv会大大降低速度吗?您的文件格式很好。鉴于您已经使用逗号分隔的值,csv解析器可能会有所帮助。我没有完全理解您的问题。我想你们对UR[z]值的不同位置有问题吧?您是否考虑在CSV模块的帮助下解析文件?(看一看)我对python相当陌生,所以如果我没有把事情说清楚或者看起来很琐碎,请原谅。因此,我们基本上希望搜索UR[z],然后在其列中返回下面的(26)个值。目前结果文件是.txt格式的,.csv会大大降低速度吗?您的文件格式很好。考虑到您已经使用逗号分隔的值,csv解析器可能会有所帮助。谢谢,可以进行排序,但问题是在我感兴趣的结果块之后还有另一个结果块使用了第[3]行,因此它也会读取它们。有没有一种方法可以在它没有收到一个值或什么东西后停止呢?谢谢,它可以工作,但问题是在我感兴趣的结果之后还有另一个结果块,它使用了第[3]行,所以它也会读取它们。有没有一种方法可以在它没有收到值或其他东西后停止?谢谢,但它只返回“'UR[z]“不在列表中。知道为什么找不到它吗?@user2739143哪一个?第一个或第二个解决方案。@user2739143再次测试了两个解决方案,它们对您提供的示例数据都很好。请发布这些解决方案不起作用的数据。我已经用全文文件编辑了原始结果谢谢mate,工作正常!虽然我试图找到列表的标准偏差,但我使用tipNodeRotationValues.append(map(str.strip,line.split(','))[column])附加了它们,然后使用np.STD(tipNodeRotationValues)计算出标准偏差,但它返回了一个错误!有什么想法吗?非常感谢!谢谢,但它只返回“'UR[z]”不在列表中”。知道它为什么找不到它吗?@user2739143哪一个?第一个或第二个解决方案。@user2739143再次测试了这两个解决方案,它们对于您提供的示例数据工作正常。请发布这些解决方案不起作用的数据。我已经用全文文件编辑了原始结果。谢谢,工作正常!虽然我试图找到列表的标准偏差,所以我使用tipNodeRotationValues.append(map(str.strip,line.split(','))[column])附加它们,然后使用np.STD(tipNodeRotationValues)计算STD,但它返回了一个错误!有什么想法吗?非常感谢!