使用python背《詩經》

《詩經》規格化txt源文件下載鏈接:https://download.csdn.net/download/github_36326955/10457910

#!usr/bin/env python  
# -*- coding:utf-8 _*-  
""" 
@project:shijing
@author:xiangguosun 
@contact:[email protected]
@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:[email protected]
@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")

效果展示
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章