如何读取小说中的七字绝句?

有些小说作者,写小说的时候喜欢引用一些古诗词。当时看着挺爽的,回头想仔细看看那些诗。但是没有那么多闲心去重新看一遍。
因此用python写个脚本统计一下文中的七字绝句。

#!/usr/bin/python
import re

f = open("msj.txt") 
line = f.readline()  
while line:
    matchObj = re.search( u"[u201cu2018]([u4e00-u9fa5]{7}[uff01uff1f][u4e00-u9fa5]{7}[u3002uff01uff1f])", line.decode("utf8"), re.M|re.I)
    if matchObj:
        print matchObj.group(1).encode("utf8")
    line = f.readline()
f.close()

把小说保存成utf-8的格式,然后执行脚本

python msj.py >> result.txt

然后自己编辑一下result,就可以得到结果了。
文中的u是为了匹配和转译中文。print的encode("utf8"),保证输出的文件是用utf8编码的。

参考网址:
https://blog.csdn.net/tianranhe/article/details/8811481
https://blog.csdn.net/IAlexanderI/article/details/79525946
https://blog.csdn.net/weixin_44521703/article/details/100187377

原文地址:https://www.cnblogs.com/bugutian/p/13125264.html