我感觉不管是在工作中还是在学习 Python 的时候, 都会到处碰壁, 这都是很常见的, 今天把会在工作中或者学习上的一些技术点总结了一下, 希望此篇文章能帮到你度过难题, 走出迷雾. 再给大家分享之前呢, 有什么不懂的问题, 都可以在群里踊跃发言, 需要啥资料随时可以私信小编, 也可以私信小编你想要获取的资料. 小编期待大家一起交流讨论,. 各种入门资料啊, 进阶资料啊, 框架资料啊 爬虫等等, 都是有的, 风里雨里, 小编等你.
我自己是一名高级 python 开发工程师, 这里有我自己整理了一套最新的 python 系统学习教程, 包括从基础的 python 脚本到 web 开发, 爬虫, 数据分析, 数据可视化, 机器学习等. 送给正在学习 python 的小伙伴! 这里是 python 学习者聚集地, 欢迎初学和进阶中的小伙伴!
- (1). import module;
- (2). __import__("some_module")
- from pymysql import connect
- class DBHelper:
- def __init__(self):
- self.conn = connect(host='localhost', port=3306, user='user', password='password', database='database', charset='utf8')
- self.csr = self.conn.cursor()
- def __enter__(self):
- return self.csr
- def __exit__(self, exc_type, exc_val, exc_tb):
- self.csr.close()
- self.conn.close()
- with DBHelper() as csr:
- sql = """select * from table;"""
- csr.execute(sql)
- all_datas = csr.fetchall()
- for item in all_datas:
- print(item)
- @contextmanager
- def conn_db():
- conn = connect(host='localhost', port=3306, user='user', password='password', database='database', charset='utf8')
- csr = conn.cursor()
- yield csr
- csr.close()
- conn.close()
- with conn_db() as csr:
- sql = """select * from table"""
- csr.execute(sql)
- all_datas = csr.fetchall()
- for item in all_datas:
- print(item)
来源: http://www.jianshu.com/p/45c218e313a3