繁体   English   中英

如何使用python将txt文件中的文本复制并重新排列到另一个文件?

[英]How can I copy and rearrange text from a txt file to another with python?

我正在尝试使用 python 将一些文本从一个文本文件复制和编辑到另一个文本文件。 我一直在环顾四周,找到了一些简单的例子,但仍然找不到我需要的一切。

我的原文是这样的(从一些文本开始,然后有一个以 NODE 开头的标题行,然后是一个以----开头的行,然后是我感兴趣的数据):

[The file starts with a lot of text, which I have not includeded here ...]
 NODE DISPLACEMENT AND ROTATIONS DEFAULT PRINTOUT                      Unit System : kN , m

__________________________________________________


 NODE       LC               UX          UY          UZ          RX    RY          RZ
------ -------- ---- ----------- ----------- ----------- ----------- ----------- -----------

   101      AW2  Max       0.005       0.000       0.001         0.0         0.0         0.0
                 Min      -0.007      -0.000      -0.000        -0.0        -0.0        -0.0

             LL  Max       0.021       0.000       0.002         0.0         0.0         0.0
                 Min      -0.031      -0.000      -0.003        -0.0        -0.0        -0.0

   102      AW2  Max       0.003       0.000       0.000         0.0         0.0         0.0
                 Min      -0.003      -0.000      -0.000        -0.0        -0.0        -0.0

我希望我的程序打印以下内容:

   101,      AW2,  Max,       0.005,       0.000,       0.001,         0.0,         0.0,         0.0
   101,      AW2,  Min,      -0.007,      -0.000,      -0.000,        -0.0,        -0.0,        -0.0
   101,       LL,  Max,       0.021,       0.000,       0.002,         0.0,         0.0,         0.0
   101,       LL,  Min,      -0.031,      -0.000,      -0.003,        -0.0,        -0.0,        -0.0
   102,      AW2,  Max,       0.003,       0.000,       0.000,         0.0,         0.0,         0.0
   102,      AW2,  Min,      -0.003,      -0.000,      -0.000,        -0.0,        -0.0,        -0.0

这是我的尝试,但它没有提供所需的输出。 我不知道如何解决这个问题:

node = 0
with open("infile.txt",'r') as inFile:
    with open("outfile.txt","w") as outFile:
        lines = inFile.read().splitlines()
        for i, line in enumerate(lines):
            if "NODE" in lines[i]:
                node = node + 1
                if node ==2:                   #it is the line "NODE  LC  UX  UY  UZ  RX  RY RZ"
                    j=3                        #it is the line "101 Aw2 Max 0.005 0.000 0.001 (...)"
                    while lines[i+j] != "\n":
                        for word in lines[i+j].split():

                         nodenumber = word[1]
                         loadcase = word[2]
                         MaxMin = word[3]
                        #How can I make it work for everyline? (they don't all have the same structure)

                        outFile.write( ) #How do I create the output that I want with comas?
                        outFile.write("\n")
                        j=j+1

你可以使用 re 来获得你想要的线条。

import re

lines = [

        ' NODE       LC               UX          UY          UZ          RX    RY          RZ',
        '------ -------- ---- ----------- ----------- ----------- ----------- ----------- -----------',
        '',
        '   101      AW2  Max       0.005       0.000       0.001         0.0         0.0         0.0',
        '                 Min      -0.007      -0.000      -0.000        -0.0        -0.0        -0.0',
        '',
        '             LL  Max       0.021       0.000       0.002         0.0         0.0         0.0',
        '                 Min      -0.031      -0.000      -0.003        -0.0        -0.0        -0.0',
        '',
        '   102      AW2  Max       0.003       0.000       0.000         0.0         0.0         0.0',
        '                 Min      -0.003      -0.000      -0.000        -0.0        -0.0        -0.0',
    ]

for line in lines:
    if re.findall(r'(Max|Min)\s+\-?\d+\.\d+\s+', line):
        print(line)

结果

101      AW2  Max       0.005       0.000       0.001         0.0         0.0         0.0
              Min      -0.007      -0.000      -0.000        -0.0        -0.0        -0.0
          LL  Max       0.021       0.000       0.002         0.0         0.0         0.0
              Min      -0.031      -0.000      -0.003        -0.0        -0.0        -0.0
102      AW2  Max       0.003       0.000       0.000         0.0         0.0         0.0
              Min      -0.003      -0.000      -0.000        -0.0        -0.0        -0.0

使用 split() 您可以将每一行的内容放在一个列表中。 这样就可以很容易地根据您的意愿重新格式化数据。

for line in lines:
    if re.findall(r'(Max|Min)\s+\-?\d+\.\d+\s+', line):
        line_parts = line.split()
        print(line_parts)

结果

['101', 'AW2', 'Max', '0.005', '0.000', '0.001', '0.0', '0.0', '0.0']
['Min', '-0.007', '-0.000', '-0.000', '-0.0', '-0.0', '-0.0']
['LL', 'Max', '0.021', '0.000', '0.002', '0.0', '0.0', '0.0']
['Min', '-0.031', '-0.000', '-0.003', '-0.0', '-0.0', '-0.0']
['102', 'AW2', 'Max', '0.003', '0.000', '0.000', '0.0', '0.0', '0.0']
['Min', '-0.003', '-0.000', '-0.000', '-0.0', '-0.0', '-0.0']

格式化

col_1 = ''
col_2 = ''
for line in lines:
    if re.findall(r'(Max|Min)\s+\-?\d+\.\d+\s+', line):
        line_parts = line.split()
        if len(line_parts) == 9:
            col_1 = line_parts[0]
            col_2 = line_parts[1]
            line_parts.pop(0)
            line_parts.pop(0)
        elif len(line_parts) == 8:
            col_2 = line_parts[0]
            line_parts.pop(0)

        str = '{:>4s}, {:>4s},'.format(col_1, col_2)
        for line_part in line_parts:
            str = str + '{:>8s},'.format(line_part)
        str = str[0:-1]
        print(str)

结果

 101,  AW2,     Max,   0.005,   0.000,   0.001,     0.0,     0.0,     0.0
 101,  AW2,     Min,  -0.007,  -0.000,  -0.000,    -0.0,    -0.0,    -0.0
 101,   LL,     Max,   0.021,   0.000,   0.002,     0.0,     0.0,     0.0
 101,   LL,     Min,  -0.031,  -0.000,  -0.003,    -0.0,    -0.0,    -0.0
 102,  AW2,     Max,   0.003,   0.000,   0.000,     0.0,     0.0,     0.0
 102,  AW2,     Min,  -0.003,  -0.000,  -0.000,    -0.0,    -0.0,    -0.0

最好使用pandas 使用内置方法pandas.read_csv() 读取数据框中的文本文件

import pandas as pd
df = pd.read_csv('filename.txt', delimiter = '\t', lineterminator ='\n')

您可能需要根据文件类型(如 utf-8 或其他内容)使用更多参数并填充列

df[column_name or number].fillna( method ='ffill', inplace = True)

上面的代码将纠正上单元格中的任何值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM