I'm a brand new Python user who wants to parse a text file that looks like this:
$ begin
$ vertex -1285.6 -226.2 -538.7 0
$ track 11 1000 0.7277 0.6765 0.1133 0
$ end
$ begin
$ vertex -1265.3 573.1 1547.7 0
$ track 11 1000 -0.7679 0.1650 0.6189 0
$ end
For every block ($ begin ... $ end) I want to get vertex coordinates x y z:
$ begin
$ vertex x y z 0
$ track 11 1000 -0.7679 0.1650 0.6189 0
$ end
Can somebody suggest a way to do this? I am very grateful for any help or advice!
Let's presume you have a text file called my file.txt with your data in it. And let's give labels to each of the items in the row:
marker = $
label = vertex OR track OR begin, etc
x = your x value
y = your y value
z = your z value
eol = the last value on the vertex line
As we read each line, we can check to see if the line includes the term 'vertex'.
If it does, we then split that line using the split function (be default, split will split on whitespace, but let's explicitly call out what we want to split on (ie. ' '). Split produces a list of elements.
We can the use tuple unpacking to break each item out of the list and assign them individual labels so our code is more readable. We can then print the values. In your case, you probably want to save or process those values... just replace the print statement with your processing code.
file = open('myfile.txt')
for line in file:
if 'vertex' in line:
fields = line.split(' ')
marker, tag, x, y, z, eol = fields
print(x, y, z)