1/从txt读取分割 

def file_list(path):
    lis = []  # 返回值配合1
    for root, dirs, files in os.walk(path):
        for filename in files:
            print(root+filename)
            file = open(root + filename, 'r', encoding="gbk")
            text = file.read()
            split = text.split('\n')
            title = split[0][4:][:-1]
            A = split[1][3:][:-1]
            B = split[2][3:][:-1]
            C = split[3][3:][:-1]
            D = split[4][3:][:-1]
            right_option = split[5][6:][:-1]
            solution = split[6][4:][:-1]
            type = split[7][7:][:-1]
            false_times = split[8][6:][:-1]
            # print(title, A, B, C, D, right_option, solution, type, false_times)

            insert_zzxzt((title, A, B, C, D, right_option, solution, type, false_times))

            file.close()

2.创建数据库

create table options(
id int unsigned primary key auto_increment not null,
title varchar(1000) not null,
A varchar(1000) not null,
B varchar(1000) not null,
C varchar(1000) not null,
D varchar(1000) not null,
right_option varchar(100) not null,
solution varchar(2000) not null,
type  varchar(100) not null,
false_times varchar(100) not null)charset=utf8mb4;

3.插入数据库

db = pymysql.connect(host='localhost', port=3306, user='root', password='123456', database='zzxzt',
                     charset='utf8mb4')
cursor = db.cursor()

def insert_zzxzt(text):
    ins = 'insert into options (title, A, B, C, D, right_option, solution, type, false_times) values (%s, %s,%s,%s,%s,%s,%s,%s, %s)'
    cursor.execute(ins, text)
    db.commit()

4.读取数据库

        self.db = pymysql.connect(host=IP, port=3306, user='root', password='123456', database='zzxzt',
                                  charset='utf8mb4')
        self.cursor = self.db.cursor()
        ins = 'select * from options'
        sql = self.cursor.execute(ins)

        self.whole_subject = self.cursor.fetchmany(sql)  # 查询的个数


        title = self.whole_subject[self.z][1]
        a = self.whole_subject[self.z][2]
        b = self.whole_subject[self.z][3]
        c = self.whole_subject[self.z][4]
        d = self.whole_subject[self.z][5]
        select_word = self.whole_subject[self.z][6]
        answer = self.whole_subject[self.z][7]
        select_type_word = self.whole_subject[self.z][8]
        self.fault_time = self.whole_subject[self.z][9]
        self.subject = title.strip()
        self.text1 = a.strip()
        self.text2 = b.strip()
        self.text3 = c.strip()
        self.text4 = d.strip()
        self.answer_word = answer.strip()
        self.select_word = select_word.strip()