python正则表达式-如何将txt文件中的一个组替换为另一个txt文件中的另一个组?
因此,我有以下txt文件: test1.txt(都在同一行中。) text2.txt(它位于两个不同的行中。) 我有下面的正则表达式模式python正则表达式-如何将txt文件中的一个组替换为另一个txt文件中的另一个组?,python,regex,Python,Regex,因此,我有以下txt文件: test1.txt(都在同一行中。) text2.txt(它位于两个不同的行中。) 我有下面的正则表达式模式 \((.*?)\) 它显然选择了括号内的所有单词 我想做的是将test1.txt中()内的单词替换为test2.txt中()内的单词,使test1.txt类似于: (This actually works)(Amazing!) 我尝试了以下代码,但似乎不起作用。我做错了什么 import re pattern = re.compile("\((.*?)\
\((.*?)\)
它显然选择了括号内的所有单词
我想做的是将test1.txt中()内的单词替换为test2.txt中()内的单词,使test1.txt类似于:
(This actually works)(Amazing!)
我尝试了以下代码,但似乎不起作用。我做错了什么
import re
pattern = re.compile("\((.*?)\)")
for line in enumerate(open("test1.txt")):
match = re.finditer(pattern, line)
for line in enumerate(open("test2.txt")):
pattern.sub(match, line)
我想我犯了一个很大的错误,这是我第一个用python编写的程序之一。你可以使用
re.sub()
的功能来允许一个可调用的替换模式,并在现场创建lambda
函数,从test2.txt
检查你的匹配以获得你的结果,例如
import re
# slightly changed to use lookahead and lookbehind groups for a proper match/substitution
pattern = re.compile(r"(?<=\()(.*?)(?=\))")
# you can also use r"(\(.*?\))" if you're preserving the brackets
with open("test2.txt", "r") as f: # open test2.txt for reading
words = pattern.findall(f.read()) # grabs all the found words in test2.txt
with open("test1.txt", "r+") as f: # open test1.txt for reading and writing
# read the content of test1.txt and replace each match with the next `words` list value
content = pattern.sub(lambda x: words.pop(0) if words else x.group(), f.read())
f.seek(0) # rewind the file to the beginning
f.write(content) # write the new, 'updated' content
f.truncate() # truncate the rest of the file (if any)
和test2.txt
包含:
(This actually works)
(Amazing!)
它还将通过迭代替换在
test2.txt
中找到的匹配项的数量来解释文件中的不匹配(例如,如果test1.txt
包含(您好)(再见)(饼状图)
,它将更改为(这实际上很有效)(惊人!)(饼状图)好的,有几个问题:
finditer
方法返回匹配对象,而不是字符串。
findall
返回匹配字符串组的列表
你做的和你说的相反。您想用test2中的数据替换test1中的数据吗
enumerate返回一个元组,使您的var行
不是一行,而是一列[行\u编号,行\u字符串\u内容]
。我在最后一个代码块中使用它
因此,您可以尝试首先捕获内容:
pattern = re.compile("\((.*?)\)")
for line in open("test2.txt"):
match = pattern.findall(line)
#match contains the list ['Amazing!'] from the last line of test2, your variable match is overwritten on each line of the file...
注意:如果编译模式,可以将其用作调用re方法的对象
如果你想一行一行地做(大文件?)。
另一个选项是加载整个文件并创建多行正则表达式
matches = []
for line in open("test2.txt"):
matches.extend(pattern.findall(line))
#matches contains the list ['This actually works','Amazing!']
然后用匹配项替换括号的内容:
for line in open("test1.txt"):
for i, match in enumerate(pattern.findall(line)):
re.sub(match, matches[i], line)
注意:如果test1.txt中的(括号中的字符串)
多于test2.txt中的,则执行此操作将引发异常
如果你想写一个输出文件,你应该这样做
with open('fileout.txt', 'w') as outfile:
for line in enumerate(open("test1.txt")):
#another writing for the same task (in one line!)
newline = [re.sub(match, matches[i], line) for i, match in enumerate(pattern.findall(line))][0]
outfile.write(newline)
非常感谢你!我想我还需要学习python和正则表达式:/
pattern = re.compile("\((.*?)\)")
for line in open("test2.txt"):
match = pattern.findall(line)
#match contains the list ['Amazing!'] from the last line of test2, your variable match is overwritten on each line of the file...
matches = []
for line in open("test2.txt"):
matches.extend(pattern.findall(line))
#matches contains the list ['This actually works','Amazing!']
for line in open("test1.txt"):
for i, match in enumerate(pattern.findall(line)):
re.sub(match, matches[i], line)
with open('fileout.txt', 'w') as outfile:
for line in enumerate(open("test1.txt")):
#another writing for the same task (in one line!)
newline = [re.sub(match, matches[i], line) for i, match in enumerate(pattern.findall(line))][0]
outfile.write(newline)