Python学习记录
2016-04-21 20:09
363 查看
环境:Ubuntu14.04,内置python3.4,Eclipse pydev.
1.数据库操作
4.Date format
5.使用别的py文件的所有方法
from 其它py的名字 import *
1.数据库操作
# official connector of mysql import mysql.connector # init connection begin config = {'host':'127.0.0.1', 'user':'root', 'password':'123', 'port':3306 , 'database':'test' } conn = mysql.connector.connect(**config) # init connection end # get cursor begin cur = conn.cursor() # get cursor end # insert begin try: cur.execute("insert into tb01(id) values(2)") conn.commit() except: conn.rollback() sql_insert_user = ("insert into user(uid,pwd)" "values(%s,%s)") user_data = ("leo", "123") try: cur.execute(sql_insert_user, user_data) conn.commit() except: conn.rollback() # insert end # select begin cur.execute("select id from tb01") # select end # result set begin rs = cur.fetchall() for el in rs: print(el[0]) # result set end # close resource begin cur.close() conn.close() # close resource end2.Http连接
import httplib conn = httplib.HTTPConnection("localhost") # conn.request("GET", "/index.html") # send request resp = conn.getresponse() # get resp print(resp.status, resp.reason, resp.read()) # read data3.JSON
import json user = { 'name':'leo', 'age':23 } print(json.dumps(user))#obj to json print(json.loads(json.dumps(user)))#json to obj
4.Date format
print datetime.now().strftime("%Y-%m-%d %H:%M:%S") #yyyy-MM-dd HH:mm:ss
5.使用别的py文件的所有方法
from 其它py的名字 import *
相关文章推荐
- python 处理中文字符串的相关问题
- python自动化测试(4)-使用第三方python库技术实现
- Python nltk Brown最有新闻价值并且是最浪漫的日子
- python—类对象和实例对象的区别
- Python基于pandas的数据处理(一)
- python 实验6 字符串练习(二)
- Python结合BeautifulSoup抓取知乎数据
- Python nltk载入自己的中文语料库的两种方法 for Windows7
- Python 高亮设置(转)
- pigpio-12c-python 学习笔记
- Python 装饰器
- python 字符串常用操作汇总
- windows下使用python的scrapy爬虫框架,爬取个人博客文章内容信息
- python实现线程池
- python基础篇
- python编程关键字
- Python下的协程
- Python 中文编码
- Python Paste Deployment
- python Queue模块