Python: 如何从文件中获取相关行?
Python: how to get the related lines from file?
如何从文件中获取相关行?
这是我的代码:
read_file = "a.txt"
read_batch = "b.txt"
lines_batch = list()
with open(read_file) as r:
bigstat = r.read()
with open(read_batch) as b:
for batch in (line_batch.strip() for line_batch in b):
if batch in bigstat:
print(???)
Bigstat 是一个包含 50 行的 txt,但我只想要其中包含批处理的 2 行。
我该怎么办?
非常感谢您的帮助!!!!!!!
这里有一些代码只使用一个 for 循环和一个 if 语句来检查 read_file 中的一行是否存在于 batch_file 中(我假设这就是你想要的检查!)。
只需打开文件并使用 readlines()
单独获取所有行。然后遍历 read_file 中的所有行并检查它们是否在 batch_file 中的行列表中(注意 readlines() 生成一个列表,其各个条目是每一行的内容,包括结束 \n
个字符)。
read_file = "a.txt"
batch_file = "b.txt"
with open(read_file) as a:
a_lines = a.readlines()
with open(batch_file) as b:
b_lines = b.readlines()
for line in a_lines:
if line in b_lines:
print(line.strip())
编辑:
要获取 read_file 中包含与 batch_file 中某行匹配的行的编号,您必须更改遍历 read_file 的方式。在这种情况下,使用 enumerate
不仅可以获取每一行的内容,还可以获取每一行的行号(本例中存储在变量 i
中)。
然后我只打印 read_file 和 batch_file 中匹配行的数量和内容。
i
获取 read_file.
中的行号
a_lines[i]
获取相应的列表项(= 行的内容)
b_lines.index(line)
获取 b_lines
列表中项目 line
的编号(= batch_file 中的行号)
line.strip()
获取 batch_file 中该行的内容,不带尾随 \n
字符。
见附件扩展代码:
read_file = "a.txt"
batch_file = "b.txt"
with open(read_file) as a:
a_lines = a.readlines()
with open(batch_file) as b:
b_lines = b.readlines()
for i, line in enumerate(a_lines):
if line in b_lines:
print("Number of line in read_file is %i" % i)
print("Content of line in read_file is %s" % a_lines[i].strip())
print("Number of line in batch_file is %i" % b_lines.index(line))
print("Content of line in batch_file is %s" % line.strip())
如何从文件中获取相关行? 这是我的代码:
read_file = "a.txt"
read_batch = "b.txt"
lines_batch = list()
with open(read_file) as r:
bigstat = r.read()
with open(read_batch) as b:
for batch in (line_batch.strip() for line_batch in b):
if batch in bigstat:
print(???)
Bigstat 是一个包含 50 行的 txt,但我只想要其中包含批处理的 2 行。
我该怎么办? 非常感谢您的帮助!!!!!!!
这里有一些代码只使用一个 for 循环和一个 if 语句来检查 read_file 中的一行是否存在于 batch_file 中(我假设这就是你想要的检查!)。
只需打开文件并使用 readlines()
单独获取所有行。然后遍历 read_file 中的所有行并检查它们是否在 batch_file 中的行列表中(注意 readlines() 生成一个列表,其各个条目是每一行的内容,包括结束 \n
个字符)。
read_file = "a.txt"
batch_file = "b.txt"
with open(read_file) as a:
a_lines = a.readlines()
with open(batch_file) as b:
b_lines = b.readlines()
for line in a_lines:
if line in b_lines:
print(line.strip())
编辑:
要获取 read_file 中包含与 batch_file 中某行匹配的行的编号,您必须更改遍历 read_file 的方式。在这种情况下,使用 enumerate
不仅可以获取每一行的内容,还可以获取每一行的行号(本例中存储在变量 i
中)。
然后我只打印 read_file 和 batch_file 中匹配行的数量和内容。
i
获取 read_file.
中的行号
a_lines[i]
获取相应的列表项(= 行的内容)
b_lines.index(line)
获取 b_lines
列表中项目 line
的编号(= batch_file 中的行号)
line.strip()
获取 batch_file 中该行的内容,不带尾随 \n
字符。
见附件扩展代码:
read_file = "a.txt"
batch_file = "b.txt"
with open(read_file) as a:
a_lines = a.readlines()
with open(batch_file) as b:
b_lines = b.readlines()
for i, line in enumerate(a_lines):
if line in b_lines:
print("Number of line in read_file is %i" % i)
print("Content of line in read_file is %s" % a_lines[i].strip())
print("Number of line in batch_file is %i" % b_lines.index(line))
print("Content of line in batch_file is %s" % line.strip())