0

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

4
  • I cannot undestand what you want lines of a single output file to look like and how it would be different from the input file. Show what it should be for the input you give. Commented Dec 13, 2016 at 1:05
  • Please see my edit above Commented Dec 13, 2016 at 1:09
  • duplicate, see: stackoverflow.com/questions/41109713/… Commented Dec 13, 2016 at 1:27
  • Well that was my question from earlier but now I'm trying to do something different... Commented Dec 13, 2016 at 1:34

2 Answers 2

1
import re
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"""
# replace all character except digit, '-', '.' and ' '(space) with nothing, get all the info you need, than split each info into a list
b = re.sub(r'[^\d\. -]','',a).split() 
# split the list to sublists, each contain four elements 
lines = [b[i:i+4] for i in range(0, len(b), 4)]
for line in lines:
    print(line)

out:

['1', '0', '-132.644', '0']
['2', '0', '-80', '0']
['3', '0', '-40', '0']
['4', '0', '0', '0']
['5', '0', '40', '0']
['6', '0', '80', '0']
['7', '0', '132.644', '0']

or write to file:

print(' '.join(line),file=open('youfilename', 'a'))

or:

with open('filename', 'w') as f:
    for line in lines:
        f.write(' '.join(line) + '\n')
    # or
    f.writelines(' '.join(line)+'\n' for line in lines)

out:

1 0 -132.644 0
2 0 -80 0
3 0 -40 0
4 0 0 0
5 0 40 0
6 0 80 0
7 0 132.644 0
Sign up to request clarification or add additional context in comments.

1 Comment

This solution is working but what if I wanted to output the results to a text file?
0

It appears you want a file with everything excepts numbers and spaces eliminated. Here are two solutions, one using REs, one not.

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()

for line in a:
    line = line.strip()
    if line and line[0].isdecimal():
        print((''.join(c for c in line if c.isdecimal() or c in '- .')
                .replace('  ', ' ')))

print()
import re
r = re.compile(r"\s*(\d+)\s+X=(\S+)\s+Y=(\S+)\s+Z=(\S+)")
for line in a:
    m = r.match(line)
    if m:
        print(' '.join(n for n in m.groups()))

This prints the following twice

1 0 -132.644 0
2 0 -80 0
3 0 -40 0
4 0 0 0
5 0 40 0
6 0 80 0
7 0 132.644 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.