[英]extracting the line with a string from a file using python
球队,
我想使用一个字符串(以tg_开头)从文件中提取一些行,并根据以下正则表达式获取输出。.问题是,
我不确定如果2行以\\
结尾,如下所示,该如何提取行。
我不知道如何使用正则表达式下面的以下内容删除特殊字符。
*****来自文件*******
tg_cr_counters dghbvcvgfv
tg_kk_bb a group1再见再见嗨嗨1 \\ <<<<
修补程序mac hdfh f dgf asadasf \\
dgfgmnhnjgfgtg_cr_counters gthghtrhgh}}] <<<<<
tg_cr_counters fkgnfkmngvd
import re
file = open("C:\\Users\\input.tcl", "r")
f1 = file.readlines()
output = open("extract.txt", "a+")
match_list = [ ]
for item in f1:
match_list = re.findall(r'[t][g][_]+\w+.*', item)
if(len(match_list)>0):
output.write(match_list[0]+"\r\n")
print(match_list)
您可以将regex与标志一起用于re.MULTILINE和re.DOTALL 。
这样一来.
也会匹配\\n
,您可以查找以tg_
(无需将每个都放在[]
)和以双\\n\\n
(或文本结尾) \\Z
结尾的任何内容:
fn = "t.txt"
with open (fn,"w") as f:
f.write("""*****from a file*******
tg_cr_counters dghbvcvgfv
tg_kk_bb a group1 bye bye bye hi hi hi 1 \ <<<<
patch mac hdfh f dgf asadasf \
dgfgmnhnjgfg
tg_cr_counters gthghtrhgh }} ] <<<<<
tg_cr_counters fkgnfkmngvd
""")
import re
with open("extract.txt", "a+") as o, open(fn) as f:
for m in re.findall(r'^tg_.*?(?:\n\n|\Z)', f.read(), flags=re.M|re.S):
o.write("-"*40+"\r\n")
o.write(m)
o.write("-"*40+"\r\n")
with open("extract.txt")as f:
print(f.read())
输出(每次匹配都在----------------------------------------
的一行之间) :
----------------------------------------
tg_cr_counters dghbvcvgfv
----------------------------------------
----------------------------------------
tg_kk_bb a group1 bye bye bye hi hi hi 1 \ <<<<
patch mac hdfh f dgf asadasf dgfgmnhnjgfg
----------------------------------------
----------------------------------------
tg_cr_counters gthghtrhgh }} ] <<<<<
----------------------------------------
----------------------------------------
tg_cr_counters fkgnfkmngvd
----------------------------------------
re.findall()
结果如下:
['tg_cr_counters dghbvcvgfv\n\n',
'tg_kk_bb a group1 bye bye bye hi hi hi 1 \\ <<<<\npatch mac hdfh f dgf asadasf dgfgmnhnjgfg\n\n',
'tg_cr_counters gthghtrhgh }} ] <<<<<\n\n',
'tg_cr_counters fkgnfkmngvd\n']
要启用多行搜索,您需要一次读入多行内容-如果文件太小,则会导致内存问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.