python读取文本文件以特定字符串开头的行

 比如读取文本文件中以[src-1]开头的行。

#encoding:utf-8
import re

file_object = open('test.txt','rU', encoding='UTF-8')
f = open('out.txt','w', encoding='UTF-8')
try:
    for line in file_object:
        g = re.search("\[src-1.*\]*", line)
        if g:
            print(g.group())
            f.writelines(line)
finally:
     file_object.close()

 

 

你可能感兴趣的:(Python)