我是python的新手,我已经浏览了多个python帖子、教程网站和源代码文档来解决我的问题,但是,我还没有完全做到这一点!
我要做的是:我有一个包含多行的文本文件,在这里我首先查找从一个"MARKERSTRING“到另一个出现的标记的文本块。"MARKERSTRING“在整个文本中多次发生,但只有少数几个在块中有"TAILSTRING”。如果找到了,那么我想添加一个新行("newstring"),就在同一块中字符串"BODY“的最后一次出现下面。
我希望将所有行保留到一个新文件中,并在给定的"BODY“索引处插入新字符串(最后一次出现在块中)。
我的文本文件的内容看起来如下:
Multiple lines with some other text
MARKERSTRING SOMESTRING SOME OTHER STRING #
BODY A B C
BODY V G H
BODY Y U I
TAILSTRING X1 Y
TAILSTRING X2 Y
MARKERSTRING SOMESTRING SOME OTHER STRING #
### #Although I want to append this to my file I dont want to process my #function through this as it does not have "TAILSTRING"
BODY B C
BODY V G H J
BODY Y U I
### #But want this block:
MARKERSTRING SOMESTRING SOME OTHER STRING #
BODY B C
BODY V G H J
TAILSTRING X1 Y
TAILSTRING X2 Y
Multiple lines with some other text结束
我的问题如下:
如果不在for循环中追加每一行,那么它们是一种更好的方法吗?
就像这样:
import re
from operator import itemgetter
import itertools
### The Function #########
def myfunc(filename):
highest = None
for cnt, line in enumerate(filename):
if line.startswith("BODY "):
bline = line.split()
highest = cnt
if line.startswith("TAIL"):
lpline = line.split()
print(lpline)
newline = "BOND", lpline[2], lpline[4]
newstring = ' '.join((str(x)) for x in newline)
bline.insert(highest + 1, newstring) ##This doesnt insert
return bline
### The "Markerstring" finder snippet: Keeps iterating over all lines #####
filename = open("input.txt").readlines()
outfilename = open("result.txt", 'w+')
buffer = []
keepCurrentSet = True
for line in filename:
buffer.append(line)
if (line.startswith('MARKERSTRING '):
if keepCurrentSet:
outfilename.write("".join(buffer))
myfunc(filename)预期结果:
Multiple lines with some other text
MARKERSTRING SOMESTRING SOME OTHER STRING #
BODY A B C
BODY V G H
BODY Y U I
BODY X1 Y #Inserted line = newstring
BODY X2 Y #Inserted line = newstring
TAILSTRING X1 Y
TAILSTRING X2 Y
MARKERSTRING SOMESTRING SOME OTHER STRING #
### #Although I want to append this to my file I dont want to process my #function through this as it does not have "TAILSTRING"
BODY B C
BODY V G H J
BODY Y U I
### #But want this block:
MARKERSTRING SOMESTRING SOME OTHER STRING #
BODY B C
BODY V G H J
BODY X1 Y #Inserted line = newstring
BODY X2 Y #Inserted line = newstring
TAILSTRING X1 Y
TAILSTRING X2 Y
Multiple lines with some other text
END发布于 2019-05-02 01:42:16
我说不出你为什么得不到你想要的结果。通常,更改或修改一两行可以解决问题。
然而,我想出了一个解决方案,我认为这是可行的。
编辑:在评论部分(下面)回答你的问题,
_, params = line.split(maxsplit = 1)
“_”是一个位置持有者,可以获得(并忽略)第一个拆分项TAILSTRING。拆分的第二个项(X1 Y或X2 Y)分配给params。
我还想确定的是,BODY X1 Y1并没有出现在我正在查看的同一块MARKERSTRING中。
为了实现这一点,需要修改代码。
fin = open('f01.txt', 'r')
fout = open('temp.txt', 'w')
buffer = []
idx = 0
for line in fin:
line = line.rstrip()
buffer.append(line)
if line.startswith('MARKERSTRING'):
for item in buffer:
fout.write(item + "\n")
buffer = []
idx = 0
# continue because don't want to increment idx at bottom of loop
# idx should be 0 for this iteration
continue
elif line.startswith('BODY'):
max_body_idx = idx
elif line.startswith('TAILSTRING'):
_, params = line.split(maxsplit = 1)
buffer.insert(max_body_idx+1, 'BODY ' + params)
max_body_idx += 1
idx += 1
fin.close()
# print out last record
for item in buffer:
fout.write(item + "\n")
fout.close()https://stackoverflow.com/questions/55942903
复制相似问题