我对python完全陌生,我有以下问题。我搜索了很多,虽然我可以找到类似的问题和答案,但我找不到一个解决我的可变性质的问题。所以是这样的:我有一个文件,在几个地方(数百)有一行读
您没有指定结果是否必须在另一个文件中,但我假设是这样。
假设foo.txt
是包含要匹配的模式的文件(即<text = " ">
),replacements.txt
包含要逐行放置的替换,这就是如何执行此任务。
import re
with open('foo.txt') as f:
lines = [line.strip() for line in f.readlines()]
with open('replacements.txt') as f:
replacements = [line.strip() for line in f.readlines()]
首先我们读取文件的内容(从空格和结束符中去掉每行)。
j = 0
for i, line in enumerate(lines):
result = re.match('<text = " ">', line)
if result and j < len(replacements):
lines[i] = replacements[j]
j += 1
然后为替换数组设置一个计数器,并对每一行搜索要替换的字符串。
如果找到了,并且需要替换,则用第j个元素替换该行。
lines = [line + 'n' for line in lines]
with open('foo_modified.txt', 'w') as f:
f.writelines(lines)
然后我们将修改的行连接在一起(手动添加结束行字符,之前删除),并将其写在另一个文件中。
作为@amquack评论和SO指南的要求,你应该有一个代码的例子,你尝试张贴在这里或至少完整的信息,你想要的文件。即使你得到了一个好的答案,你也应该用更完整的信息编辑问题,并在这里接受它。
当我读到你的问题时,你有一个文件,比如f1
,看起来像
a
bunch
<text = " ">
of
<text = " ">
other
lines
<text = " ">
和另一个文件f2
看起来像这样
string1
string2
string3
,并且您希望将f1
中读取<text = " ">
的行替换为f2
中的行。这里有一种可能性,假设f2
包含的行数与f1
<text = " ">
行数相同。# get the contents of your files using the with statement so
# the interpreter cleans up after you
with open('f1', 'r') as infile:
f1_lines = infile.readlines()
with open('f2', 'r') as infile:
f2_lines = infile.readlines()
# create a new file for output
with open('out', 'w') as outfile:
# loop over the lines in the text you want to modify
for line in f1_lines:
if line.strip() == '<text = " ">':
# this is a line you want to replace, so pop the next
# line you want to insert off of f2_lines and write
# it to the output file
outfile.write(f2_lines.pop(0))
if len(f2_lines) == 0:
# if that was the last line to insert, add a newline
# character in case there are more lines to copy over
# from f1
outfile.write('n')
else:
# the text you want to replace is not in this line, so
# copy this line to the output file
outfile.write(line)
注意,如果你有非常大的文件,或者你在大量的文件上运行这段代码,那么for循环和f2_lines.pop
将不像其他方法那样执行得好。上面的文件内容和代码生成的文件如下所示
a
bunch
string1
of
string2
other
lines
string3