Akira_Z

 

python习题(二)

题目描述:

将一份文件中的特殊字符都找出来,存到另一份文件中。

解法:

导入re模块,使用正则表达式筛选特殊字符。

处理过程中要注意文件的格式转换,如有中文不处理格式会乱码,另外还要将换行符过滤掉。

import re
with open('test.txt', 'r', encoding='utf-8') as f_in:
    data = f_in.read()
    new = re.findall(r'\W', data)
    print(new)
with open('result.txt', 'w+') as f_out:
    for i in range(len(new)):
        s = str(new[i]).replace('\n', '')
        f_out.write(s)

 

posted on 2020-05-18 21:10  Akira_Z  阅读(151)  评论(0编辑  收藏  举报

导航