代码:-*- coding:utf-8 -*-import re#读取1.txt中文件,按行替换'Book'+任意字符 为空,写入2.txt中with open('1.txt','r') as f1: with open('2.txt','w') as f2: for i in f1: f2.write(re.sub('Book\w','', i))
Python读取txt文字,撷取并输出成想要的格式
使用正则表达式替换即可。
代码:
# -*- coding:utf-8 -*-import re#读取1.txt中文件,按行替换'Book'+任意字符 为空,写入2.txt中with open('1.txt','r') as f1: with open('2.txt','w') as f2: for i in f1: f2.write(re.sub('Book\w','', i))2016-05-04