我有一个这种格式的文件:
ELEM MAT TYP REL ESY SEC NODES
1 1 1 1 0 1 611 2856 618 2582 94075 94107 94065 94068
101071 94104
2 1 1 1 0 1 598 2856 618 611 93995 94107 93992 93991
94075 94065
ELEM MAT TYP REL ESY SEC NODES
37561 29 29 1 0 29 32186 32642 32666 32210 32187 32643 32667 32211
191297 192707 191370 191296 191300 192710 191373 191299
191295 192706 192779 191368
37562 29 29 1 0 29 32187 32643 32667 32211 32188 32644 32668 32212
191300 192710 191373 191299 191303 192713 191376 191302
191298 192709 192782 191371
37563 29 29 1 0 29 32188 32644 32668 32212 32189 32645 32669 32213
191303 192713 191376 191302 191305 192715 191378 191304
191301 192712 192785 191374
我需要的是:
1 1 1 1 0 1 611 2856 618 2582 94075 94107 94065 94068 101071 94104
2 1 1 1 0 1 598 2856 618 611 93995 94107 93992 93991 94075 94065
37561 29 29 1 0 29 32186 32642 32666 32210 32187 32643 32667 32211 191297 192707 191370 191296 191300 192710 191373 191299 191295 192706 192779 191368
37562 29 29 1 0 29 32187 32643 32667 32211 32188 32644 32668 32212 191300 192710 191373 191299 191303 192713 191376 191302 191298 192709 192782 191371
37563 29 29 1 0 29 32188 32644 32668 32212 32189 32645 32669 32213 191303 192713 191376 191302 191305 192715 191378 191304 191301 192712 192785 191374
我需要删除文本并将其余信息写入在线。通过使用excel进行后处理也可以删除文本。最大的问题是以行的形式写入数据,但有些数据用两行写入,另外一些用三行写入。这些文件包含有限元模型的元素编号和节点编号。我需要在每个元素之后写入节点。任何帮助表示赞赏。
答案 0 :(得分:1)
import re
with open('untitled.txt') as f:
# untitled.txt contains the input text.
last_line = []
for line in f:
m = re.match('ELEM', line)
if m:
# if line has header, ignore and continue
continue
current_numbers = re.findall(r'\d+', line)
m = re.match('^\s{20,30}', line)
if m:
# if line starts with a lot of spaces add values to previous line
last_line.extend(current_numbers)
else:
if last_line:
# if we have a previous line print it/ write it to file.
print(last_line)
# assign new line found as last line and continue with the loop
last_line = current_numbers
if last_line:
print(last_line)
输出:
['1', '1', '1', '1', '0', '1', '611', '2856', '618', '2582', '94075', '94107', '94065', '94068', '101071', '94104']
['2', '1', '1', '1', '0', '1', '598', '2856', '618', '611', '93995', '94107', '93992', '93991', '94075', '94065']
['37561', '29', '29', '1', '0', '29', '32186', '32642', '32666', '32210', '32187', '32643', '32667', '32211', '191297', '192707', '191370', '191296', '191300', '192710', '191373', '191299', '191295', '192706', '192779', '191368']
['37562', '29', '29', '1', '0', '29', '32187', '32643', '32667', '32211', '32188', '32644', '32668', '32212', '191300', '192710', '191373', '191299', '191303', '192713', '191376', '191302', '191298', '192709', '192782', '191371']
['37563', '29', '29', '1', '0', '29', '32188', '32644', '32668', '32212', '32189', '32645', '32669', '32213', '191303', '192713', '191376', '191302', '191305', '192715', '191378', '191304', '191301', '192712', '192785', '191374']
PS:你可以选择任何格式来编写它,这取决于你。
答案 1 :(得分:0)
首先,基础知识:
data_out=open(filename,"r")
data_in=open(filename+"_output.txt","w")
for line in data_out:
written=False
if [...]
如果它是一个"核心线"那么你可以选择一个序列来使代码识别出来。 (如1 1 1 1 0 1 611 2856 618 2582 94075 94107 94065 94068)或紧随其后的一条线(94075 94065)。您可以通过REL / ESY参数检查它,例如boolean-ish。宿醉线不包含这样的东西。如果一行包含" 0"或" 1"你可以将它保存为变量。只要没有新的核心线,就可以追加以下几行。
将其打印到新文档
data_in.write(line)
别忘了
data_in.close()
data_out.close()