“绿”
获取所有含有“绿”的古诗
请注意,需要预置json
和opencc
两个库
安装opencc:pip install opencc-python-reimplemented
from json import loads
from opencc import OpenCC
f = open('./log','w')
f.close()
all = ''
def log(text):
print(text)
return text+'\n'
cc = OpenCC('t2s') # 繁转简
count = 0
# 唐诗三百首
with open('./全唐诗/唐诗三百首.json','r',encoding='utf-8') as f:
tssbs = loads(f.read())
for poem in tssbs:
for line in poem['paragraphs']:
if '綠' in line or '绿' in line:
count+=1
all+=log(cc.convert('{line}————《{title}》{author}'.format(line=line,title=poem['title'],author=poem['author'])))
# song
for number in range(0,254000,1000):
with open('./全唐诗/poet.song.{n}.json'.format(n=str(number)),'r',encoding='utf-8') as f:
poems = loads(f.read())
for poem in poems:
for line in poem['paragraphs']:
if '綠' in line or '绿' in line:
count+=1
all+=log(cc.convert('{line}————《{title}》{author}'.format(line=line,title=poem['title'],author=poem['author'])))
# tang
for number in range(0,57000,1000):
with open('./全唐诗/poet.tang.{n}.json'.format(n=str(number)),'r',encoding='utf-8') as f:
poems = loads(f.read())
for poem in poems:
for line in poem['paragraphs']:
if '綠' in line or '绿' in line:
count+=1
all+=log(cc.convert('{line}————《{title}》{author}'.format(line=line,title=poem['title'],author=poem['author'])))
# shijing
with open('./诗经/shijing.json','r',encoding='utf-8') as f:
poems = loads(f.read())
for poem in poems:
for line in poem['content']:
if '綠' in line or '绿' in line:
count+=1
all+=log(cc.convert('{line}————《{t}》'.format(line=line,t='诗经·'+poem['chapter']+'·'+poem['section'])))
# 防止重复
endresult = []
for i in all.split('\n'):
if i not in endresult:
endresult.append(i)
else:
count -= 1
all = '\n'.join(endresult)
print('扫描完成,含“绿”的诗句共有{c}句'.format(c=str(count)))
with open('./log','a',encoding='utf-8') as logfile:
logfile.write(all)
正确运行代码后,在当前目录会有一个log
文件,文件示例在
最后更新于