废话少说直接看代码
import re
# 读取源文件
f = open('dd.txt', 'rb') # 以二进制读出
count = f.read()
counts = count.decode('utf8') # 在将原数据转换为utf-8标准
# 处理文件(直接在数据中提取中文)
zh = u"([\u4e00-\u9fff]+)" # 中文的正则标准
re_words = re.compile(zh)
results = re_words.findall(counts)
# 创建并写入
new_path = ".\\" # 当前路径
new_file = new_path + "dd2.txt" # 当前路径加文件名
f = open(new_file, 'w') # 打开新文件,如果没有直接创建
# f.writelines(results) # 直接将列表里的中文一口气写入进文件里
for i in results: # 使用循环一行一行写入文件中
f.write(i)
f.write('\n')
f.close()
# 提取用户idcard的正则匹配方法
# re.findall(r"(\d{17}[\d|x|X])", user_id)