当前位置: 代码迷 >> python >> 读取行并在python中以另一种格式写入它们
  详细解决方案

读取行并在python中以另一种格式写入它们

热度:64   发布时间:2023-06-13 15:05:49.0

我有一个这种格式的文件:

    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 进行后处理来删除。 最大的问题是将数据写成行,但有些数据写成两行,而另一些则写成三行。 这些文件包含有限元模型的元素编号和节点编号。 我需要在每个元素之后写入节点。 任何帮助表示赞赏。

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:您可以选择任何格式来编写它,这取决于您。

一、基础知识:

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 来检查它。 宿醉行不包含这样的东西。 如果一行包含“ 0 ”或“ 1 ”,您可以将其保存为变量。 只要没有新的核心行,就添加以下行。

将其打印到新文档中

data_in.write(line)

不要忘记

data_in.close()
data_out.close()