python笔记 模块导入

参考:http://www.cnblogs.com/wupeiqi/articles/4276448.html
模块导入

import os
os.getcwd()

##################

from os import *
getcwd()

##################
'字符串相关'

mode = __import__('os')
mode.getcwd()

'判断是否存在方法'

hasattr(mode,'getcwd')
#返回True

重新导入

import os

"""
......
"""

reload(os)

加密

import hashlib
hash = hashlib.md5()
hash.update(b'admin')
print(hash.hexdigest())

#'21232f297a57a5a743894a0e4a801fc3'
#无法解密,设计密码验证,将输入密码加密后与真是密码加密后字串进行对比

pickle,json

>>> import pickle,json
>>> data = {'k1':123,'k2':'hello'}
>>> p_str = pickle.dumps(data)
>>> p_str
b'\x80\x03}q\x00(X\x02\x00\x00\x00k1q\x01K{X\x02\x00\x00\x00k2q\x02X\x05\x00\x00\x00helloq\x03u.'
>>> j_str = json.dumps(data)
>>> j_str
'{"k1": 123, "k2": "hello"}'

#json 兼容性较高

Time

import time
#1、时间戳    1970年1月1日之后的秒
#3、元组 包含了:年、日、星期等... time.struct_time
#4、格式化的字符串    2014-11-11 11:11
 
print(time.time())
#数值  1535964087.0532687

print(time.localtime())
#time.struct_time类 
#time.struct_time(tm_year=2018, tm_mon=9, tm_mday=3, tm_hour=16, tm_min=42, tm_sec=18, tm_wday=0, tm_yday=246, tm_isdst=0)

print(time.mktime(time.localtime()))
#数值 1535964243.0
 
print(time.gmtime() )   #可加时间戳参数 格林?
print(time.localtime()) #可加时间戳参数 本时区
print(time.strptime('2014-11-11', '%Y-%m-%d'))
#time_struct_time类
#time.struct_time(tm_year=2014, tm_mon=11, tm_mday=11, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=315, tm_isdst=-1)
 
print(time.strftime('%Y-%m-%d')) #默认当前时间 '18-09-03'
print(time.strftime('%Y-%m-%d',time.localtime())) #默认当前时间 '18-09-03'

print(time.asctime())
#Mon Sep  3 16:47:45 2018
print(time.asctime(time.localtime()))

print(time.ctime())
#Mon Sep  3 16:49:10 2018
print(time.ctime(time.time()))
# Mon Sep  3 16:49:41 2018



import datetime
'''
datetime.date:表示日期的类。常用的属性有year, month, day
datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond
datetime.datetime:表示日期时间
datetime.timedelta:表示时间间隔,即两个时间点之间的长度
timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
strftime("%Y-%m-%d")
'''
import datetime
print(datetime.datetime.now())
#datetime.datetime(2018, 9, 3, 16, 51, 27, 394050)

print datetime.datetime.now() - datetime.timedelta(days=5) #??

补充

# \w 表示 下划线_字母abc中横杆——
# + 表示个数>=1     *表示个数 >= 0
# ? 0 或 1
# {m} 出现次数  'A{6}'   A出现6次
# {sm,n} 出现次数 'A{3,5}'  A出现3~5(包括5)次
>>> import re

#match 从头开始,匹配成功返回对象,否则返回none
#search 从头开始,若未匹配成功则跳过继续匹配,直到匹配成功返回对象,否则返回none
>>> mh1 = re.match('\d+','123qwe123qwe')
>>> sh1 = re.search('\d+','123qwe123qwe')
>>> mh2 = re.match('\d+','qwe123qwe123')
>>> sh2 = re.search('\d+','qwe123qwe123')
>>> 
>>> import _sre
>>> mh1.group()
'123'
>>> sh1.group()
'123'
>>> mh2.group()
Traceback (most recent call last):
  File "<pyshell#158>", line 1, in <module>
    mh2.group()
AttributeError: 'NoneType' object has no attribute 'group'
>>> sh2.group()
'123'

#findall 列表形式返回所有匹配成功的对象,否则返回空列表
>>> f_a = re.findall('\d+','qwe123qwe123')
>>> f_a
['123', '123']

#compile 第一次使用需要编译,之后不要,提高效率
#match本质上先compile再findall
>>> com = re.compile('\d+')
>>> type(com)
<class '_sre.SRE_Pattern'>
>>> com.findall('qwe123qwe123')
['123', '123']

#groups,group
#groups提取pattern中匹配成功括号()的内容
>>> sh2 = re.search('\d+','qwe123qwe123')
>>> sh2.groups()
()
>>> sh2.group()
'123'
>>> sh3 = re.search('(\d+)\w*(\d+)','qwe123q—we123')
>>> sh4 = re.search('(\d+)\w*(\d+)','qwe123q——we123')
>>> sh3.group()
'123'
>>> sh3.groups()
('12', '3')
>>> sh4.group()
'123'
>>> sh4.groups()
('12', '3')
 

猜你喜欢

转载自blog.csdn.net/weixin_41948344/article/details/82320734