python练习册 每天一个小程序 第0002题
1 #-*-coding:utf-8-*-
2 __author__ = 'Deen'
3 '''
4 题目描述:
5 将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。
6 '''
7 """
8 import MySQLdb as mdb
9
10 config = {
11 'host': '127.0.0.1',
12 'port': 3306,
13 'user': 'root',
14 'passwd': '',
15 'db': 'student',
16 'charset': 'utf8'
17 }
18 conn = mdb.connect(**config)
19 cursor = conn.cursor(cursorclass=mdb.cursors.DictCursor)
20
21
22
23 cursor.execute('set names gbk')
24
25
26
27 n=0
28 num=list()
29
30
31 for line in open("student12.txt"):
32 line=line.decode('gbk').encode('utf-8')
33
34 n=n+1
35
36 if n%6==1:
37 cursor.execute('insert into student12(sno) VALUES (%s)'%(line))
38
39 """
40
41 import MySQLdb as mdb
42
43 config = {
44 'host': '127.0.0.1',
45 'port': 3306,
46 'user': 'root',
47 'passwd': '',
48 'db': 'ctf_test',
49 'charset': 'utf8'
50 }
51 conn = mdb.connect(**config)
52 cursor = conn.cursor(cursorclass=mdb.cursors.DictCursor)
53 cursor.execute('set names gbk')
54
55 cursor.execute('create table codes(id int(5),code char(20))')
56 with open('poll_codes.txt','r') as fp:
57 for line in fp.readlines():
58 line = line.strip('\n').split(':')
59 cursor.execute('INSERT INTO codes VALUES (%s,%s)',line)
60
61
62 conn.commit()
63 cursor.close()
64 conn.close()
python练习册 每天一个小程序 第0002题的更多相关文章
- python练习册 每天一个小程序 第0013题
# -*-coding:utf-8-*- ''' 题目描述: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) 地址: http://tieba.baidu.com/p/21 ...
- python练习册 每天一个小程序 第0001题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生 ...
- python练习册 每天一个小程序 第0007题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但 ...
- python练习册 每天一个小程序 第0000题
PIL库学习链接:http://blog.csdn.net/column/details/pythonpil.html?&page=1 1 #-*-coding:utf-8-*- 2 __au ...
- python练习册 每天一个小程序 第0010题
# -*-coding:utf-8-*- ''' 题目描述: 使用 Python 生成类似于下图中的字母验证码图片 思路: 运用PIL库加random 随机字母进行生成 ''' import rand ...
- python练习册 每天一个小程序 第0009题
1 ''' 2 题目描述: 3 找出一个html文件中所有的url 4 5 思路 : 6 利用正则表达式进行匹配 7 8 ''' 9 10 11 import re 12 13 14 with ope ...
- python练习册 每天一个小程序 第0008题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 一个HTML文件,找出里面的正文. 6 7 思路: 8 利用Beautiful ...
- python练习册 每天一个小程序 第0006题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目描述: 5 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都 ...
- python练习册 每天一个小程序 第0005题
1 # -*-coding:utf-8-*- 2 __author__ = 'Deen' 3 ''' 4 题目说明: 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小 ...
随机推荐
- 1、架构--架构图、Iptables(简介、四表五链、流程图、使用、扩展模块)、包过滤防火墙
笔记 1.画架构图 2.Iptables 1.1 什么是防火墙 防止别人恶意访问. 1.2 防火墙种类 硬件防火墙 F5 软件防火墙 iptables firewalld 安全组 3.Iptables ...
- Solution -「UOJ #450」复读机
\(\mathcal{Description}\) Link. 求从 \(m\) 种颜色,每种颜色无限多的小球里选 \(n\) 个构成排列,使得每种颜色出现次数为 \(d\) 的倍数的排列方案 ...
- mysql悬案 之 为什么用docker启动的mysql配置文件不生效
文章目录 故事前景 查看docker启动时挂载了哪些目录 使用相同镜像启动一个mysql 新建一个目录用来存放容器内的mysql配置文件 复制容器内的mysql配置文件到本地 查看mysql配置文件目 ...
- 2020年的第一天-我的IDEA出现This license ... has been cancelled
IDEA激活在1月3日的早上,激活码被取消了.提示:This license ... has been cancelled. 经过查询.解决方法教程无非是. ¥%--&*(激活码... 210 ...
- MySQL数据库备份的4种方式
MySQL备份的4种方式 总结: 备份方法 备份速度 恢复速度 便捷性 功能 一般用于 cp 快 快 一般.灵活性低 很弱 少量数据备份 mysqldump 慢 慢 一般.可无视存储引擎的差异 一般 ...
- 创建jsp页面出现The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误
原因未添加tomcat服务器 第一步: 第二步:
- k8s-ingress增加跨域问题
第一种: kubectl get ingresses. -n rrzhibo-admin test-rrzb-apiadmin-gateway-http-ingress -o yaml apiVers ...
- 部署Nginx网站服务
编译安装Nginx nginx 官方 : http://nginx.org/ yum -y install pcre-devel zlib-devel ##安装相关依赖包 ./configure ...
- Ubuntu安装g++命令
Ubuntu安装g++ sudo apt-get install make gcc g++ 再装上函数手册 sudo apt-get install manpages-dev 或者采用 sudo ap ...
- 学习Spring5必知必会(5)~Spring AOP
一.学习 AOP 思想的准备工作: 1.横切面关注点 在开发中,为了给业务方法中增加日志记录,权限检查,事务控制等功能,此时我们需要在修改业务方法内添加这些零散的功能代码(横切面关注点). 这些零散存 ...