[英]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.我正在尝试使用 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):我的原文是这样的(从一些文本开始,然后有一个以 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
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.你可以使用 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)
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.使用 split() 您可以将每一行的内容放在一个列表中。 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()最好使用pandas 使用内置方法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您可能需要根据文件类型(如 utf-8 或其他内容)使用更多参数并填充列
df[column_name or number].fillna( method ='ffill', inplace = True)
the above code will right the value whatever is in the upper cell上面的代码将纠正上单元格中的任何值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.