一、time&datetime模块
import timeprint(time.time()) #1554370227.6189237 秒, 时间戳,从1970年1月1日 00:00开始到现在,1970年是Unix元年t = time.localtime() #结构化时间,本地时间print(t) #time.struct_time(tm_year=2019, tm_mon=4, tm_mday=4, tm_hour=17, tm_min=39, tm_sec=40, tm_wday=3, tm_yday=94, tm_isdst=0)print(t.tm_year) #2019 ,取年份print(time.localtime(1554371025.1826413)) #将当前时间转换为结构化时间t2 = time.gmtime() #结构化时间,UTC时间print(t2) #time.struct_time(tm_year=2019, tm_mon=4, tm_mday=4, tm_hour=9, tm_min=39, tm_sec=40, tm_wday=3, tm_yday=94, tm_isdst=0)print(time.mktime(time.localtime())) #将结构化时间转换为时间戳print(time.strftime("%Y-%m-%d %X", time.localtime())) #2019-04-04 18:16:04 将结构化时间转换为字符串时间print(time.strptime("2019-04-04 18:16:04", "%Y-%m-%d %H:%M:%S")) #time.struct_time(tm_year=2019, tm_mon=4, tm_mday=4, tm_hour=18, tm_min=16, tm_sec=4, tm_wday=3, tm_yday=94, tm_isdst=-1) 将字符串时间转换为结构化时间print(time.asctime()) #将结构化时间转换为固定的格式, Mon Apr 8 15:47:59 2019print(time.ctime()) #将时间戳时间转换为固定的格式, Mon Apr 8 15:48:48 2019import datetimeprint(datetime.datetime.now()) #2019-04-08 15:54:06.410871
二、random模块
import randomprint(random.random()) #0 ~ 1之间的浮点数print(random.randint(1,10)) #1 ~ 10之间的整数print(random.randrange(1,10)) #1 ~ 10之间的整数,不包括10print(random.choice([11,22,33])) #在列表中随机取1个元素print(random.sample([11,22,33],2)) #在列表中随机取2个元素print(random.uniform(1,5)) #1 ~ 5之间的浮点数l = [1,2,3,4,5]random.shuffle(l) #洗牌print(l) #[4, 1, 3, 5, 2]
- 随机验证码代码示例
import randomdef v_code(): ret = "" for i in range(5): num = random.randint(0,9) alf = chr(random.randint(65,90)) s = str(random.choice([num,alf])) ret += s return retprint(v_code())
三、BASE_DIR
import sys,osBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))print(BASE_DIR) #D:\VirtualBox VMs\data\pyobj\1904sys.path.append(BASE_DIR)print(sys.path) #['D:\\VirtualBox VMs\\data\\pyobj\\1904\\0404', 'D:\\VirtualBox VMs\\data\\pyobj\\1904', 'C:\\Python\\Python35\\python35.zip', 'C:\\Python\\Python35\\DLLs', 'C:\\Python\\Python35\\lib', 'C:\\Python\\Python35', 'C:\\Python\\Python35\\lib\\site-packages', 'D:\\VirtualBox VMs\\data\\pyobj\\1904']
四、os模块
import osprint(os.getcwd()) #获取当前工作目录,D:\VirtualBox VMs\data\pyobj\1904\0404os.chdir("D:\\VirtualBox VMs\\data\\pyobj\\1904\\") #cd命令print(os.getcwd()) #D:\VirtualBox VMs\data\pyobj\1904# 目录操作os.makedirs("dir1/dir2/dir3") #mkdir -p dir1/dir2/dir3os.removedirs("dir1/dir2/dir3") #rmdir -p dir1/dir2/dir3os.mkdir("dir1") #mkdir dir1os.rmdir("dir1") #rmdir dir1print(os.listdir("0403")) #ls -a# 文件操作os.remove("test.txt") #删除一个文件os.rename("test.txt", "new_test.txt")print(os.stat("new_test.txt")) #文件的元数据,os.stat_result(st_mode=33206, st_ino=9570149208162616, st_dev=2457279955, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1554785068, st_mtime=1554785068, st_ctime=1554785068)print(os.path.getatime("new_test.txt"))print(os.path.getmtime("new_test.txt"))print(os.path.getctime("new_test.txt"))# 其他print(os.sep) #路径分隔符print(os.linesep) #换行符print(os.pathsep) #路径分隔符,比如环境变量的配置print(os.name) #操作系统的名字print(os.system("ipconfig")) #执行操作系统命令# 路径print(os.path.split(os.getcwd())) #('D:\\VirtualBox VMs\\data\\pyobj\\1904', '0404')print(os.path.dirname(os.getcwd())) #D:\VirtualBox VMs\data\pyobj\1904print(os.path.basename(os.getcwd())) #0404print(os.path.exists("D:\\dir1\\test.txt")) #判断路径是否存在print(os.path.isabs("D:\\dir1\\test.txt")) #是否是绝对路径print(os.path.isfile("D:\\dir1\\test.txt")) #是否是一个文件a = "D:\\dir1"b = "test.txt"print(os.path.join(a,b)) #路径拼接,D:\dir1\test.txt
五、sys模块
import sysprint(sys.argv) #命令行参数列表,第一个元素是程序本身路径,['D:/VirtualBox VMs/data/pyobj/1904/0404/模块.py']print(sys.version) #python版本,3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)]sys.stdout.write('#') #打印到屏幕,#sys.exit(1) #退出程序print(sys.path) #打印环境变量print(sys.platform) #打印操作系统平台名称
- 进度条
import time,sysfor i in range(30): sys.stdout.write('#') time.sleep(0.1) sys.stdout.flush()
六、json、pickle和shelve模块
- json的应用(dumps和loads)
import jsondic = {"name":"dongfei", "age":18, "gender":"M"}data = json.dumps(dic)print(data, type(data)) #{"name": "dongfei", "age": 18, "gender": "M"}# with open("test.json", "w", encoding="utf-8") as f:# f.write(data)with open("test.json", "r", encoding="utf-8") as f: new_data = json.loads(f.read()) print(new_data,type(new_data)) #{'age': 18, 'name': 'dongfei', 'gender': 'M'}
- json的应用(dump和load)
import jsondic = {"name":"dongfei", "age":18, "gender":"M"}with open("test2.json", "w", encoding="utf-8") as f: json.dump(dic,f)with open("test2.json", "r", encoding="utf-8") as f: new_data = json.load(f) print(new_data,type(new_data)) #{'gender': 'M', 'name': 'dongfei', 'age': 18}
pickle用法和json完全一样,但是pickle支持序列化函数和类,但是只能在python程序间使用,pickle文本不可读
shelve使用方法(pickle的高级封装)
import shelvef = shelve.open(r"test4.txt")f["stu1_info"] = {"name":"dongfei", "age":18, "gender":"M"}print(f.get("stu1_info")["age"]) #18
七、xml模块
- test5.xml
2 2008 141100 5 2011 59900 69 2011 13600
- xml的操作
import xml.etree.ElementTree as ETtree = ET.parse("test5.xml")root = tree.getroot() #获取根节点print(root.tag) #根标签名字,data# 遍历xml文档for child in root: print(child.tag, child.attrib) for i in child: print(i.tag, i.text) #text取内容# 遍历year节点for node in root.iter("year"): print(node.tag, node.text)# 修改for node in root.iter("year"): new_year = int(node.text) + 1 node.text = str(new_year) #修改内容 node.set("update", "yes") #修改属性值tree.write("test6.xml")# 删除for country in root.findall("country"): #findall查找文档中所有 rank = int(country.find("rank").text) if rank > 50: root.remove(country)tree.write("test7.xml")
- 创建xml数据
import xml.etree.ElementTree as ETnew_xml = ET.Element("namelist") #namelist 是根节点名name = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})age = ET.SubElement(name, "age", attrib={"checked":"no"})sex = ET.SubElement(name, "sex")sex.text = 'M'name2 = ET.SubElement(new_xml, "name", attrib={"enrolled":"no"})age = ET.SubElement(name2, "age")age.text = "18"et = ET.ElementTree(new_xml) #生成文档对象et.write("test8.xml", encoding="utf-8", xml_declaration=True)ET.dump(new_xml) #打印生成的文档对象
- test8.xml
M 18
八、re模块
1、正则表达式元字符
- . 任意一个字符
- ^,$ 行首/尾锚定
- * 0或无穷次
- + 1或无穷次
- ? 0或1次
- {3,5} 3或4或5次
- [] 字符集
[a-z] 表示所有小写字母[^a-z] 表示所有小写字符外的字符re.findall("\([^()]*\)","12+(23*5+2-5*(4-2))") #['(4-2)'] 匹配最里边的括号和内容
- \ 转义符
\d 匹配任何十进制数;它相当于类 [0-9]\D 匹配任何非数字字符;它相当于类 [^0-9]\s 匹配任何空白字符;它相当于类 [ \t\n\r\f\v]\S 匹配任何非空白字符;它相当于类 [^ \t\n\r\f\v]\w 匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]\W 匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]\b 匹配一个特殊字符边界,比如空格 ,&,#等,单词锚定>>> re.findall("I\\b","hello I am list")['I']>>> re.findall(r"I\b","hello I am list") #r 的意思是原生字符串,里边的内容不需要让python解释器翻译['I']
- | 或
- () 分组,将括号内的变成一个整体
>>> re.search(r"(?P[a-z]+)\d+","hello33 I am list").group("str_name") #后向引用'hello'
2、re模块使用
import reprint(re.findall("\d+", "asdf123alsdkjflwk332alskjdfkl55kk")) #查询字符串中所有匹配到的元素,并返回一个列表print(re.search(r"(?P[a-z]+)\d+","hello33 I am list").group("str_name")) #匹配到第一个匹配到的元素,返回一个对象print(re.match("abc","abc123abc")) #相当于在search基础上加了^行首锚定print(re.split("[,|]","abc,sdf,dfs|jkljks|sd")) #使用特定的分隔符讲字符串分割后返回列表print(re.sub("\d","B","abc123sdf33ds")) #将匹配到的做替换操作,abcBBBsdfBBdsprint(re.subn("\d","B","abc123sdf33ds")) #('abcBBBsdfBBds', 5)rule = re.compile("\d+") #编译print(rule.findall("asdf123alsdkjflwk332alskjdfkl55kk")) #['123', '332', '55']iter1 = re.finditer("\d+","asdf123alsdkjflwk332alskjdfkl55kk") #返回一个迭代器对象print(next(iter1).group()) #123print(re.findall("www\.(baidu|qq|aliyun)\.com","www.baidu.com")) #['baidu']print(re.findall("www\.(?:baidu|qq|aliyun)\.com","www.baidu.com")) #?: 去优先级,['www.baidu.com']
九、logging模块
- basicConfig
import logginglogging.basicConfig( level=logging.INFO, #定义日志记录级别 filename="dongfei.log", #存放到文件中 filemode="w", #日志记录模式,默认是追加模式 format="%(asctime)s %(filename)s [%(lineno)d] %(message)s")logging.debug("debug message")logging.info("info message")logging.warning("warning message")logging.error("error message")logging.critical("critical message")
- format参数中可能用到的格式化串:
%(name)s Logger的名字%(levelno)s 数字形式的日志级别%(levelname)s 文本形式的日志级别%(pathname)s 调用日志输出函数的模块的完整路径名,可能没有%(filename)s 调用日志输出函数的模块的文件名%(module)s 调用日志输出函数的模块名%(funcName)s 调用日志输出函数的函数名%(lineno)d 调用日志输出函数的语句所在的代码行%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示%(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒%(thread)d 线程ID。可能没有%(threadName)s 线程名。可能没有%(process)d 进程ID。可能没有%(message)s用户输出的消息
- logger对象
import loggingdef logger(logfile): logger = logging.getLogger("mylogger") logger.setLevel("INFO") #设置日志级别 fh = logging.FileHandler(logfile) #向文件输出 ch = logging.StreamHandler() #向屏幕输出 fm = logging.Formatter("%(asctime)s %(message)s") #自定义格式 fh.setFormatter(fm) ch.setFormatter(fm) logger.addHandler(fh) logger.addHandler(ch) return loggerlogger = logger("dongfei2.log") #实例化logger.debug("debug message")logger.info("info message")logger.warning("warning message")logger.error("error message")logger.critical("critical message")
十、configparser模块
- 应用
import configparserconfig = configparser.ConfigParser() #实例化config["DEFAULT"] = { "ServerAliveInterval": 45, "Compression": "yes", "CompressionLevel": 9}with open("dongfei2.conf", "w") as f: config.write(f)
- confile.conf
[DEFAULT]ServerAliveInterval = 45Compression = yesCompressionLevel = 9ForwardX11 = yes[bitbucket.org]User = hg[topsecret.server.com]Port = 50022ForwardX11 = no
- 查
import configparserconfig = configparser.ConfigParser()config.read("confile.conf")# 查print(config.sections()) #查询除default外的所有键['bitbucket.org', 'topsecret.server.com']print(config["bitbucket.org"]["User"]) #查 ,hgprint(config["DEFAULT"]["CompressionLevel"]) #9for key in config["bitbucket.org"]: #无论遍历哪个键都会显示default段 print(key)print(config.options("bitbucket.org")) #['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']print(config.items("bitbucket.org")) #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]print(config.get("bitbucket.org","CompressionLevel")) #9
- 增删改
import configparserconfig = configparser.ConfigParser()config.read("confile.conf")config.add_section("open.org") #增键config.set("open.org","k1","v1") #增值config.remove_section("topsecret.server.com") #删键config.remove_option("DEFAULT","compression") #删值config.set("DEFAULT","serveraliveinterval","56") #改config.write(open("confile2.conf","w"))
十一、hashlib模块
import hashlibobj = hashlib.md5("with salt".encode("utf-8"))obj.update("dongfei123".encode("utf-8"))print(obj.hexdigest()) #ace17212e78240faf0773e40d8d4f268
十二、paramiko模块
- 基于密码ssh连接
# Author:Li Dongfeiimport paramikossh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(hostname='192.168.56.8', port=22, username='root', password='dongfei')stdin, stdout, stderr = ssh.exec_command('df')result = stdout.read()ssh.close()
- 基于密钥ssh连接
# Author:Li Dongfeiimport paramikoprivate_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')ssh = paramiko.SSHClient()ssh.connect(hostname='192.168.56.8', port=22, username='root', pkey=private_key)stdin, stdout, stderr = ssh.exec_command('df')result = stdout.read()print(result.decode())ssh.close()
- 基于密码sftp传输文件
# Author:Li Dongfeiimport paramikotransport = paramiko.Transport(('192.168.56.8', 22))transport.connect(username='root', password='dongfei')sftp = paramiko.SFTPClient.from_transport(transport)sftp.get('/root/back.sql','back.sql')transport.close()