首页 / 操作系统 / Linux / 用Python随机生成数据再插入到PostgreSQL中
用Python随机生成学生姓名,三科成绩和班级数据,再插入到PostgreSQL中。模块用psycopg2 randomimport randomimport psycopg2fname=["金","赵","李","陈","许","龙","王","高","张","侯","艾","钱","孙","周","郑"]mname=["玉","明","玲","淑","偑","艳","大","小","风","雨","雪","天","水","奇","鲸","米","晓","泽","恩","葛","玄","道","振","隆","奇"]lname=["","玲","","芳","明","红","国","芬","","云","娴","隐","","花","叶","","黄","亮","锦","茑","军","","印","","凯"]# 数据库连接参数conn = psycopg2.connect(database="girl", user="jm", password="123", host="127.0.0.1", port="5432")cur = conn.cursor()for x in range(200):#生成随机数据 pname=random.choice(fname)+random.choice(mname)+random.choice(lname)math=random.randint(40,100)english=random.randint(40,100)chinese=random.randint(40,100)pclass=random.randint(1,3)#插入数据 (特别注意只能用%s不能用%d,数值型数据不用引号cur.execute("insert into score values(%s,%s,%s,%s,%s,%s)",(x,pname,math,english,chinese,glass))conn.commit()#提交命令,否则数据库不执行插入操作cur.close()conn.close() random.choice(序列):在一个序列中随机选取一个元素Ubuntu 14.04安装Python 3.3.5 http://www.linuxidc.com/Linux/2014-05/101481.htmCentOS上源码安装Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htmPython脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htmPython 的详细介绍:请点这里
Python 的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/133668.htm