使用python+txt构建测试数据-创新互联

一、背景

创新互联服务项目包括寻乌网站建设、寻乌网站制作、寻乌网页制作以及寻乌网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,寻乌网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到寻乌省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

   有4张表,每张表要插入多条测试数据。如若还有同种需求,于是写了一个脚本,来添加数据。

二、代码

#--coding:utf8--
import pymysql


class InsertTestData(object):
    STUDENT_FILE = 'Student.txt'
    COURSE_FILE = 'Course.txt'
    TEACHER_FILE = 'Teacher.txt'
    SCORE_FILE = 'Score.txt'

    def __init__(self):
        self.connect = pymysql.Connect(
            host = 'localhost',
            port = 3306,
            user = 'root',
            # passwd = ' ',
            charset = 'utf8'
        )
        self.database = 'execersise_test'

    def read_lines(self, filename):
        dict = {}
        file_name = filename.split('.')[0]
        dict['file_name'] = file_name
        with open(filename) as f:
            lines = f.readlines()
        dict['file_content'] = lines
        return dict

    def connect_mysql(self):
        cursor = self.connect.cursor()
        return cursor

    def close_mysql(self):
        self.connect.close()

    def close_curser(self):
        self.connect_mysql().close()

    def add_test_datas(self, file_obj):
        file = file_obj
        file_name = file['file_name']
        title = file['file_content'][0].strip()
        datas = file['file_content'][1:]
        content_len = len(datas)
        data = ''
        counter = 1
        for tmpdata in datas:
            if counter == content_len:
                data += '(' + tmpdata.strip() + ');'
            else:
                data += '(' + tmpdata.strip() + '),'
            counter += 1
        sql = 'insert into ' + self.database + '.' + file_name + ' (' + title + ') values '+ data
        try:
            # self.connect_mysql().executemany(sql)
            self.connect_mysql().execute(sql)
        except Exception as e:
            print('add_' + file_name + ' error: ', e)
        self.connect.commit()
        self.close_curser()

if __name__ == '__main__':
    testdata = InsertTestData()
    testdata.add_test_datas(testdata.read_lines(InsertTestData.STUDENT_FILE))
    testdata.add_test_datas(testdata.read_lines(InsertTestData.TEACHER_FILE))
    testdata.add_test_datas(testdata.read_lines(InsertTestData.COURSE_FILE))
    testdata.add_test_datas(testdata.read_lines(InsertTestData.SCORE_FILE))

   在main函数中,只需要指定要读取的文件,即可快速插入测试数据。

   为了便于构造sql语句,定义的数据文件格式如下:

`SID`,`CID`,`Degree` 
'103' , '3-245' , '86'
'105' , '3-245' , '75'
'109' , '3-245' , '68'
'103' , '3-105' , '92'
'105' , '3-105' , '88'
'109' , '3-105' , '76'
'101' , '3-105' , '64'
'107' , '3-105' , '91'
'108' , '3-105' , '78'
'101' , '6-166' , '85'
'107' , '6-166' , '79'
'108' , '6-166' , '81'

三、遇到的问题

  1. UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence

    原因:文件编码不是ANSI;

    解决方法:将文件用notepad打开,另存,编码设置为ANSI格式。

  2. 入库的中文信息乱码,显示为'?'

    原因:在创建数据库时,未将charset设置为utf8;

    解决方法:重建数据库,重建表,数据库和表的charset都设置为utf8

  3. 如何将sql的values数据构造成‘(),(),();‘的格式

    解决方法:增加计数器,当读取到最后一行时,以‘;’结尾

  4. 如何通过读取一个文件,同时获取要操作的表名称、列名、values

    解决方法:

    数据文件的文件名称,定义成表名;

    数据文件的titile,即第一行内容,定义成表的列;

    数据文件的内容,即第一行之后的内容,定义成表的数据。

网站标题:使用python+txt构建测试数据-创新互联
文章出自:https://www.cdcxhl.com/article10/ceeido.html

成都网站建设公司_创新互联,为您提供企业网站制作定制开发网站改版外贸建站网站内链企业建站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管