简体   繁体   中英

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

I'm trying to copy and edit some text from one text file to another using python. I've been looking around and found some simple examples but still can't find everything I need.

My original text is something like this (starting with some text, then having a header line starting with NODE, followed by a line starting with ---- , and then comes the data that I am interested in):

[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

I want my program to print the following:

   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

This is my attempt, but it does not give the desired output. I don't know how to address this problem:

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

You could use re to get the lines you want.

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)

result

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

and with split() you can put the contents of each line in a list. With that it should be easy to to reformat the data to your wishes.

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

result

['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']

formatted

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)

result

 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

better to use pandas to read text file in data frame using built in method pandas.read_csv()

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

you might need to use some more arguments according to file type like utf-8 or something else and fill columns

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

the above code will right the value whatever is in the upper cell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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