我正在使用python。我有一个文本文件,格式如下:
###########
text lines
###########
text lines
###########
text lines
###########
我想为两个" ######"之间找到的每个段运行一个算法。线。 如何引用两个" ######"之间的文本行。线。 两个" ######"之间的行数。线不固定。
感谢
答案 0 :(得分:1)
您可以使用split()
轻松完成此操作:
with open('myfile.txt') as f:
data = f.read().split('###########')
print([txt.strip() for txt in data if txt])
答案 1 :(得分:0)
这会有用吗?
f = open("textfile.txt")
for line in f.readlines():
if '######' not in line:
print(line) # evaluate and process the line here
答案 2 :(得分:0)
或者这个怎么样:
with open("f.txt") as f:
print(''.join(x for x in filter(lambda x: x != '###########', f.read().split("\n"))))
答案 3 :(得分:0)
file.txt
:
###########
line1
#
line2
######
line3
line4
line5
##########
line6
line7
代码:
import re
FILE_PATH = "file.txt"
blocks = []
lines = []
with open(FILE_PATH) as file:
for line in file:
if re.fullmatch(r"#+(\n){0,1}", line):
if lines:
blocks.append(lines)
lines = []
else:
lines.append(line.rstrip("\n")) # remove rstrip if you want to keep a new line at the end of a line
# store last block (if file does not end with #+ line)
if lines:
blocks.append(lines)
print(blocks) # [['line1'], ['line2'], ['line3', 'line4', 'line5'], ['line6', 'line7']]
答案 4 :(得分:0)
考虑“######”不包含“\ n”:
def get_chunks(filename):
with open(filename) as f:
return f.read().split('#'*6)[2:-1:2]
否则:
import re
def get_chunks(filename):
with open(filename) as f:
return re.split(r'(#\n?){6}', f.read())[4:-1:4]