Reputation: 621
I'm trying to import a list of numbers from a results text file using python. Problem is the location of the list varies depending on the size of the data required. My part of my results file I'm interested in looks like this......
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
So essentially I want to make a list of the values in the UR[z] column, but the location of this block varies depending the on the amount of nodes etc. So I was thinking of say searching for UR[z] then returning the values in its column. Also I would like to do it with out specifying the number of nodes, but can do it if it simplifies it greatly.
Any help would be greatly appreciated! Cheers!
Just as a further note the rest of the results file is as follows!
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
I only want to looks at the tip rotation data section!
Upvotes: 1
Views: 89
Reputation: 251136
Without using any modules:
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]
output:
-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
Update:
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]
output:
-2.6071
-2.6071
-2.6053
-2.5948
-2.5697
-2.5279
-2.4701
-2.3998
...
-2.5948
-2.6053
Upvotes: 2
Reputation: 31090
You could use numpy genfromtxt:
import numpy as np
data = np.genfromtxt('yourfile.txt',delimiter=' , ',skip_header=1, names=True)
Then access your column like this
data['URx']
Upvotes: 1
Reputation: 764
You could do something like this (a big rough on the float parsing though):
import csv
with open('data.txt','r') as fh:
reader = csv.reader(fh)
for row in reader:
try:
print float(row[3])
except: pass
Upvotes: 1