在某些单词之间阅读并打印出中间的内容。 (Python)
Read between certain words and print out whats in between. (Python)
您好,我是 python 编程的新手,我想知道如何修复我的代码以允许我只读取普通文本文档中两个字符串之间的内容。例如假设我有以下
unimportant data
unimportant data
unimportant data
... ... ...
First string #I want to print out starting from this line
Important data
Important data
Important data
Important data
Last String #Last line I dont want to print this line.
unimportant data
unimportant data
unimportant data
unimportant data
unimportant data
unimportant data
到目前为止,我已经能够制作一个简单的文件 I/O 来读取一行。
data_file = open("test.txt", "r")
for line in data_file:
if re.match("(.*)First String(.*)", line):
print(line)
但是这只会打印出第一行。
如有任何提示或帮助,我们将不胜感激。
go = False
start = "First string"
end = "Last String"
with open('path/to/file') as infile:
for line in infile:
line = line.strip()
if line == start: go = True
elif line == end:
go = False
continue
if go: print(line)
如果您只是在寻找关键字,而不是匹配整行:
go = False
start = "First string"
end = "Last String"
with open('path/to/file') as infile:
for line in infile:
line = line.strip()
if start in line: go = True
elif end in line:
go = False
continue
if go: print(line)
您可以使用 itertools.dropwhile
:
from itertools import dropwhile
def find_section(fle, start, end):
from itertools import dropwhile
with open(fle) as f:
for line in dropwhile(lambda x: not x.startswith(start), f):
if line.startswith(end):
break
yield line
for line in find_section("in.txt", "First String ", "Last string "):
print(line)
输出:
First string #I want to print out starting from this line
Important data
Important data
Important data
或者结合 takewhile 和 dropwhile:
from itertools import dropwhile, takewhile
def find_section(fle, start, end):
with open(fle) as f:
for line in takewhile(lambda x: not x.startswith(start),
dropwhile(lambda x: not x.startswith(end), f)):
yield line
或者只使用内部循环:
def find_section(fle, start, end):
with open(fle) as f:
for line in f:
if line.startswith(start):
yield line
for _line in f:
if _line.startswith(end):
return
yield _line
for line in find_section("in.txt", "First string", "Last String "):
print(line)
输出:
First string #I want to print out starting from this line
Important data
Important data
Important data
Important data
无论你想匹配什么条件,只需在 lambda 中使用它,替换 startswith 逻辑,同样使用 for 循环
您好,我是 python 编程的新手,我想知道如何修复我的代码以允许我只读取普通文本文档中两个字符串之间的内容。例如假设我有以下
unimportant data
unimportant data
unimportant data
... ... ...
First string #I want to print out starting from this line
Important data
Important data
Important data
Important data
Last String #Last line I dont want to print this line.
unimportant data
unimportant data
unimportant data
unimportant data
unimportant data
unimportant data
到目前为止,我已经能够制作一个简单的文件 I/O 来读取一行。
data_file = open("test.txt", "r")
for line in data_file:
if re.match("(.*)First String(.*)", line):
print(line)
但是这只会打印出第一行。
如有任何提示或帮助,我们将不胜感激。
go = False
start = "First string"
end = "Last String"
with open('path/to/file') as infile:
for line in infile:
line = line.strip()
if line == start: go = True
elif line == end:
go = False
continue
if go: print(line)
如果您只是在寻找关键字,而不是匹配整行:
go = False
start = "First string"
end = "Last String"
with open('path/to/file') as infile:
for line in infile:
line = line.strip()
if start in line: go = True
elif end in line:
go = False
continue
if go: print(line)
您可以使用 itertools.dropwhile
:
from itertools import dropwhile
def find_section(fle, start, end):
from itertools import dropwhile
with open(fle) as f:
for line in dropwhile(lambda x: not x.startswith(start), f):
if line.startswith(end):
break
yield line
for line in find_section("in.txt", "First String ", "Last string "):
print(line)
输出:
First string #I want to print out starting from this line
Important data
Important data
Important data
或者结合 takewhile 和 dropwhile:
from itertools import dropwhile, takewhile
def find_section(fle, start, end):
with open(fle) as f:
for line in takewhile(lambda x: not x.startswith(start),
dropwhile(lambda x: not x.startswith(end), f)):
yield line
或者只使用内部循环:
def find_section(fle, start, end):
with open(fle) as f:
for line in f:
if line.startswith(start):
yield line
for _line in f:
if _line.startswith(end):
return
yield _line
for line in find_section("in.txt", "First string", "Last String "):
print(line)
输出:
First string #I want to print out starting from this line
Important data
Important data
Important data
Important data
无论你想匹配什么条件,只需在 lambda 中使用它,替换 startswith 逻辑,同样使用 for 循环