I have a python script that reads a list of text and writes it to four seperate files. Would it be possible to just write one text file where each line has the joint coordinate x coordinate y coordinate and z coordinate separated by spaces? I would greatly appreciate any help
import os
os.chdir('/Users/JP/DevEnv/ASA')
import re
# regex to extract data line
r = re.compile(r"\s*(\d+)\s+X=(\S+)\s+Y=(\S+)\s+Z=(\S+)")
a="""SYSTEM
DOF=UY,UZ,RX LENGTH=FT FORCE=Kip
JOINT
1 X=0 Y=-132.644 Z=0
2 X=0 Y=-80 Z=0
3 X=0 Y=-40 Z=0
4 X=0 Y=0 Z=0
5 X=0 Y=40 Z=0
6 X=0 Y=80 Z=0
7 X=0 Y=132.644 Z=0""".splitlines().__iter__()
# open all 4 files with a meaningful name
files=[open("file_{}.txt".format(x),"w") for x in ["J","X","Y","Z"]]
for line in a:
m = r.match(line)
if m:
# line matches: write in all 4 files (using zip to avoid doing
# it one by one)
for f,v in zip(files,m.groups()):
f.write(v+"\n")
# close all output files now that it's done
for f in files:
f.close()
The first line of the output text file would look like this: 1 0 -132.644 0