很久没有写blog了,现在将我一直以来的总结贴出来,希望对大家用有。现在作业比较多,没有时间事理了。
import sqlite3
conn=sqlite3.connect('test.db')
cur=conn.cursor()
cur.execute("create table test (id int,char1 varchar(10),char2 varchar(10),da text)")
cur.execute("insert into test values (1,'a','b','chhfhfhh')")
t='aaaa'
cur.execute("insert into test values (5,'%s','%s','%s')" % (t,t,t)) # 插入变量
cur.execute('select * from test') 查询
a=cur.fetchall()
for i in a:
print i
conn.commit() 提交,相当于保存
cur.close()
conn.close()
2进制到10进制:
int(str(1011), 2)
10进制到2进制:
def bin(num):
if num == 0: return '0' return "".join([str((num>>i)&1) for i in xrange(int(math.floor(math.log(num, 2))),-1,-1)])
import PIL.Image
PIL.Image.open('d:\\a.jpg').save('d:\\b.gif')
转换jpg文件为gif
import random
import string
#随机整数:
print random.randint(1,50)
#随机选取0到100间的偶数:
print random.randrange(0, 101, 2)
#随机浮点数:
print random.random()
print random.uniform(1, 10)
#随机字符:
print random.choice('abcdefghijklmnopqrstuvwxyz!@#$%^&*()')
#多个字符中选取特定数量的字符:
print random.sample('zyxwvutsrqponmlkjihgfedcba',5)
#多个字符中选取特定数量的字符组成新字符串:
print string.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5)).replace(' ','')
#随机选取字符串:
print random.choice(['剪刀', '石头', '布'])
#打乱排序
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print random.shuffle(items)
数据压缩
import zlib
s='witch which has which whih abc google'
len(s)
t=zlib.compress(s)
len(t)
r=zlib.decompress(t)
zlib.crc32(s)
import tkFileDialog, os, sys
filename = tkFileDialog.askopenfilename(initialdir=os.getcwd())
相当于WINDOWS的浏览,选择文件
sys.setdefaultencoding('utf8')
正则表达式
s='abcdefg'
ss=re.sub(r'(.)',r'\1 ',s)
ss='a b c d e f g'
import os #查找目录
a=os.listdir(os.getcwd())
for i in a:
if os.path.isdir(i):
print i
复制对象
import copy
a=[1,3,5]
b=copy.copy(a)
c=a[:]
组合
s='abcdefg'
for i in range(len(s)):
for j in range(len(s)-i+1):
print s[i:i+j]
urllib.urlopen(url, proxies={'http' : 'http://address:port'}) 使用代理
import sys
sys.path.append('/home/lcg/mylib') 添加路径
退出
exit()
不用import sys了
如果是在while里面,只要break
import sqlite3
conn=sqlite3.connect('test.db')
cur=conn.cursor()
cur.execute("create table test (id int,char1 varchar(10),char2 varchar(10),da text)")
cur.execute("insert into test values (1,'a','b','chhfhfhh')")
t='aaaa'
cur.execute("insert into test values (5,'%s','%s','%s')" % (t,t,t)) # 插入变量
cur.execute('select * from test') 查询
a=cur.fetchall()
for i in a:
print i
conn.commit() 提交,相当于保存
cur.close()
conn.close()
2进制到10进制:
int(str(1011), 2)
10进制到2进制:
def bin(num):
if num == 0: return '0' return "".join([str((num>>i)&1) for i in xrange(int(math.floor(math.log(num, 2))),-1,-1)])
import PIL.Image
PIL.Image.open('d:\\a.jpg').save('d:\\b.gif')
转换jpg文件为gif
import random
import string
#随机整数:
print random.randint(1,50)
#随机选取0到100间的偶数:
print random.randrange(0, 101, 2)
#随机浮点数:
print random.random()
print random.uniform(1, 10)
#随机字符:
print random.choice('abcdefghijklmnopqrstuvwxyz!@#$%^&*()')
#多个字符中选取特定数量的字符:
print random.sample('zyxwvutsrqponmlkjihgfedcba',5)
#多个字符中选取特定数量的字符组成新字符串:
print string.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5)).replace(' ','')
#随机选取字符串:
print random.choice(['剪刀', '石头', '布'])
#打乱排序
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print random.shuffle(items)
数据压缩
import zlib
s='witch which has which whih abc google'
len(s)
t=zlib.compress(s)
len(t)
r=zlib.decompress(t)
zlib.crc32(s)
import tkFileDialog, os, sys
filename = tkFileDialog.askopenfilename(initialdir=os.getcwd())
相当于WINDOWS的浏览,选择文件
sys.setdefaultencoding('utf8')
正则表达式
s='abcdefg'
ss=re.sub(r'(.)',r'\1 ',s)
ss='a b c d e f g'
import os #查找目录
a=os.listdir(os.getcwd())
for i in a:
if os.path.isdir(i):
print i
复制对象
import copy
a=[1,3,5]
b=copy.copy(a)
c=a[:]
组合
s='abcdefg'
for i in range(len(s)):
for j in range(len(s)-i+1):
print s[i:i+j]
urllib.urlopen(url, proxies={'http' : 'http://address:port'}) 使用代理
import sys
sys.path.append('/home/lcg/mylib') 添加路径
退出
exit()
不用import sys了
如果是在while里面,只要break