python之常用的数据处理方法

1.生成6位数验证码

"".join([random.choice(chars) for i in range(6)])

2.密码加密

import hashlib
def encryptPwd(pwd, salt):
    h1 = hashlib.md5()
    dst = pwd + salt
    h1.update(dst.encode("utf8"))
    pwdEncode = h1.hexdigest()
    return pwdEncode.upper()
oldPwd='123'
newPwd=encryptPwd('123', '加盐')
print(newPwd)
#7843D1005A1BE1C6606D555E7993E249

3.姓名首字母排序

from pypinyin import lazy_pinyin
def pYinNameMap(names):
    pyin_name_tuples = [(''.join(lazy_pinyin(n)), n) for n in names]
    pyinName = {}

    ss = sorted(pyin_name_tuples, key=lambda x:x[0])
    for item in ss:
        firChar = item[0][0]
        name = item[1]
        if firChar not in pyinName.keys():
            pyinName[firChar] = [name]
        else:
            pyinName[firChar].append(name)
    return pyinName

pyinName=pYinNameMap(['张三','李四'])
print(pyinName)
#{'l': ['李四'], 'z': ['张三']}

4.时间转为字符串类型的日期"2018年10月18日"

import datetime
i=datetime.datetime.now().date()
print('%s年%s月%s日'%(i.year,i.month,i.day))
#2018年10月18日

5.字符串日期取年月日

def dateToymd(date):
    (y, m, d) = map(int, date.split('-'))
    return (y, m, d)
print(dateToymd("2018-1-3"))
#(2018, 1, 3)

6.字符串日期返回一星期的第几天

date.weekday()返回的0-6代表周一--到周日

import datetime
def dateToymd(date):
    (y, m, d) = map(int, date.split('-'))
    return (y, m, d)

def WeekToDay(date):
    ymd = dateToymd(date)
    print(ymd)
    return datetime.date(*ymd).weekday()

print(WeekToDay("2018-10-18"))

#(2018, 10, 18)
#3

datetime.datetime.isoweekday()返回的1-7代表周一--周日;

import datetime
def dateToymd(date):
    (y, m, d) = map(int, date.split('-'))
    return (y, m, d)

def WeekToisoday(date):
    ymd = dateToymd(date)
    print(ymd)
    return datetime.date(*ymd).isoweekday()

print(WeekToisoday("2018-10-18"))

#(2018, 10, 18)
#4

 

7.返回两个字符串日期之前的所有日期,并且以列表的形式,eg: '2018-09-02', '2018-09-04' 返回 ['2018-09-02', '2018-09-03', '2018-09-04']

8.一天的一段时间之后

from datetime import datetime
str='2018-12-11'
timeNow=datetime.strptime(str,'%Y-%m-%d')
print(timeNow)
EndDate = timeNow.replace(hour=23, minute=59, second=59, microsecond=59)
print(EndDate)

#2018-12-11 00:00:00
#2018-12-11 23:59:59.000059

 

未完待续......

 

posted on 2018-10-18 20:59  V神丫丫  阅读(369)  评论(0编辑  收藏  举报