来自如下结构的文件:
..............................
Delimiter [1]
..............................
blablabla
..............................
Delimiter CEO [2]
..............................
blabla
..............................
Delimiter [3]
..............................
[...]
..............................
Delimiter CEO [n-1]
..............................
blablabla
..............................
Delimiter [n]
..............................
我编写了一个代码来提取所有分隔符,但也提取了一些我不需要的行。我不需要的那些行 my code无法正常运行。 我想在新的 .txt 文件中保存一行,如果该行中有正则表达式“[a number]”。因此,为了更准确地提取,我使用 re: 在 python 中编写了这段代码(在 this answer 之后):
import re
with open('testoestratto.txt','r',encoding='UTF-8') as myFile:
text = myFile.readlines()
text = [frase.rstrip('\n') for frase in text]
regex = r'\[\d+\]'
new_file=[]
for lines in text:
match = re.search(regex, lines, re.MULTILINE)
if match:
new_line = match.group() + '\n'
new_file.append(new_line)
with open('prova.txt', 'w') as f:
f.seek(0)
f.writelines(new_file)
但是,在“prova.txt”文件中,我只能找到正则表达式,因此我有一个包含 [1]、[2]、... [n-1]、[n] 的文件。
最佳答案
您的 new_file
是文件中找到的匹配项的列表(您用 match.group()
+ 换行符填充)。
您可以检查一行中是否有 \[\d+]
匹配并将该行输出到新文件中:
import re
reg = re.compile(r'\[\d+]') # Matches a [ char, followed with 1+ digits and then ]
with open('prova.txt', 'w') as f: # open file for writing
with open('testoestratto.txt','r',encoding='UTF-8') as myFile: # open file for reading
for line in myFile: # read myFile line by line
if reg.search(line): # if there is a match anywhere in a line
f.write(line) # write the line into the new file
关于python - RegEx,在一行中找到匹配项并打印它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51458478/