我有一个python脚本,可读取文本列表并将其写入四个单独的文件。是否可以只编写一个文本文件,其中每行的联合坐标x坐标y坐标和z坐标用空格分隔?我将不胜感激任何帮助
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()
输出文本文件的第一行如下所示:1 0 -132.644 0
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)
出去:
['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']
或写入文件:
print(' '.join(line),file=open('youfilename', 'a'))
或者:
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)
出去:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句