去发现生活中的美好,记录生活中的点点滴滴

python实用技巧汇总

python admin 608℃

1、python md5加密:

str="aa"
md5 = hashlib.md5()
md5.update(str.encode('utf-8'))
sign = md5.hexdigest()

2、python去掉小数点后面多余的0:

print('{:g}'.format(0.2000))
# 0.2

3、判断数据类型:

a = 1
b = [1,2,3,4]
c = (1,2,3,4)
d = {‘a‘:1,‘b‘:2,‘c‘:3}
e = "aaa"
if isinstance(a,int):
    print "a is int"
if isinstance(b,list):
    print "b is list"
if isinstance(c,tuple):
    print "c is tuple"
if isinstance(d,dict):
    print "d is dict"
if isinstance(e,str):
    print "d is str"

4、获取字符串中的数字:

extract_digit = lambda x: re.sub("[^\d\.]", "", x)
s = "这是数字123!"
number = extract_digit(s) #123


转载请注明:永盟博客 » python实用技巧汇总

喜欢 (3)