使用python背《诗经》

《诗经》规格化txt源文件下载链接:https://download.csdn.net/download/github_36326955/10457910

#!usr/bin/env python  
# -*- coding:utf-8 _*-  
""" 
@project:shijing
@author:xiangguosun 
@contact:sunxiangguodut@qq.com
@website:http://blog.csdn.net/github_36326955
@file: writejson.py 
@platform: macOS High Sierra 10.13.1 Pycharm pro 2017.1 
@time: 2018/06/04 
"""

import json

with open('source', 'r') as readfile:
    data = readfile.readlines()


source_dic = {

}

with open('source.json', 'w') as wj:
    old_category = ''
    new_category = '国风·周南'
    cate_changed = False
    poem_changed = False
    old_name = ''
    new_name = '关雎'
    content = ''
    count = -1
    blank = False

    number = 0

    for line in data:

        if "**" in line:
            old_category = new_category
            new_category = line[2:-1]
            cate_changed = True
        elif "○" in line:
            number = number + 1
            old_name = new_name
            new_name = line[1:-1]
            poem_changed = True
        elif line == "\n":
            blank = True
            continue
        else:
            content = content + line
            poem_changed = False

        # print(old_category)
        # print(new_category)

        if poem_changed and (new_name != old_name):
            count = count + 1
            source_dic[count] = {"category": old_category,
                                 "name": old_name,
                                 "content": content
                                 }
            content = ''

    wj.write(json.dumps(source_dic))

#!usr/bin/env python  
# -*- coding:utf-8 _*-  
""" 
@project:shijing
@author:xiangguosun 
@contact:sunxiangguodut@qq.com
@website:http://blog.csdn.net/github_36326955
@file: main.py 
@platform: macOS High Sierra 10.13.1 Pycharm pro 2017.1 
@time: 2018/06/04 
"""

import random
import json

with open('source.json', 'r') as rf:
    data = json.loads(rf.read())

number = int(input("how many items you want to include..."))

selects = random.choices(list(data.keys()), k=number)

print("We selected", number, " items randomly to test you. They are...")

for item in selects:
    print("《 ",data[item]['name']," 》")

print("Now you should recite them one by one, each time after you finished,\n", "you can press any key to print the answer...")

for item in selects:
    print("Now Recite")
    print("《 ",data[item]['name']," 》", "...")
    ifend = input("you can press any key to show answer after you finish")
    print(data[item]['content'],"\n")

效果展示
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值