[python学习]在txt文件中筛选特殊字符和其所在句

新手刚学py,参考了一下大佬的代码,结合自己的想法写的查找txt文件方法。
旨在记录每天学习的内容

def copyChun():
        # with open(file="dict.txt",mode="r",encoding=utf8) as afile:
    s = "风"
    print("Loading...")

    with open(file=r"全唐诗.txt",mode='r',encoding='utf8') as srcfile:
        for line in srcfile:
            str = srcfile.readline()
            if s in str:
                with open(file=r"风.txt",mode="a",encoding='utf8') as dstfile:
                    dstfile.write(str)
                    dstfile.close()
                # else:
                #     print("这行没有正在跳转下一行")

def copyYv():
    s = "雨"
    print("Loading...")

    with open(file=r"全唐诗.txt",mode='r',encoding='utf8') as srcfile:
        for line in srcfile:
            str = srcfile.readline()
            if s in str:
                with open(file=r"雨.txt",mode="a",encoding='utf8') as dstfile:
                    dstfile.write(str)
                    dstfile.close()


                # else:
                #     print("这行没有正在跳转下一行")

    s = "润物"
    print("Loading...")

    with open(file=r"全唐诗.txt",mode='r',encoding='utf8') as srcfile:
        for line in srcfile:
            str = srcfile.readline()
            if s in str:
                with open(file=r"雨.txt",mode="a",encoding='utf8') as dstfile:
                    dstfile.write(str)
                    dstfile.close()

copyChun()
copyYv()
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值