Python基础篇-day6

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础篇-day6相关的知识,希望对你有一定的参考价值。

本节简介:

1、模块
1.1 时间模块
1.2 random模块
1.3 shutil模块
1.4 shelve模块
1.5 XML模块
1.6 ConfigParser模块
1.7 hashlib模块
1.8 logging模块
1.9 re模块

 

1、模块

1.1 时间模块

time
print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
print(time.altzone) #返回与utc时间的时间差,以秒计算\
print(time.asctime()) #返回时间格式"Fri Aug 19 11:14:16 2016",
print(time.localtime()) #返回本地时间 的struct time对象格式
print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式
print(time.asctime(time.localtime())) #返回时间格式"Fri Aug 19 11:14:16 2016",
print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上
日期字符串 转成 时间戳
string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
print(string_2_struct)

struct_2_stamp = time.mktime(string_2_struct) #将struct时间对象转成时间戳
print(struct_2_stamp)

将时间戳转为字符串格式
print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式

时间加减
import datetime
print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19
print(datetime.datetime.now() )
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
c_time = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换

1.2 random模块
>>> import random
>>> print(random.random())
0.208189160969
>>> print(random.randint(1,5)) #随机打印1-5数据
5
>>> print(random.randrange(1,5)) #随机打印1-4
1
>>> import string
>>> str_source = string.ascii_letters + string.digits
>>> print(random.sample(str_source,7)) #随机打印str_source中的7个字符
[‘s‘, ‘E‘, ‘w‘, ‘Z‘, ‘h‘, ‘I‘, ‘N‘]

生成随机验证码>>:
import random
checkcode = ‘‘
for i in range(4): #四位随机码
current = random.randrange(0,4) #思维随机码
if current != i:
temp = chr(random.randint(65,90))
else:
temp = random.randint(0,9)
checkcode += str(temp)
print checkcode

1.3 shutil模块

shutil.copyfileobj(fsrc, fdst[, length])
将文件内容拷贝到另一个文件中,可以部分内容

shutil.copyfile(src, dst)
拷贝文件

shutil.copymode(src, dst)
仅拷贝权限。内容、组、用户均不变

shutil.copystat(src, dst)
拷贝状态的信息,包括:mode bits, atime, mtime, flags

shutil.copy(src, dst)
拷贝文件和权限

shutil.copy2(src, dst)
拷贝文件和状态信息

shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件

shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件

shutil.move(src, dst)
递归的去移动文件

shutil.make_archive(base_name, format,...)

创建压缩包并返回文件路径,例如:zip、tar

    • base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
      如:www                        =>保存至当前路径
      如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
    • format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
    • root_dir: 要压缩的文件夹路径(默认当前目录)
    • owner: 用户,默认当前用户
    • group: 组,默认当前组
    • logger: 用于记录日志,通常是logging.Logger对象
shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:
import zipfile

# 压缩
z = zipfile.ZipFile(‘laxi.zip‘, ‘w‘)
z.write(‘a.log‘)
z.write(‘data.data‘)
z.close()

# 解压
z = zipfile.ZipFile(‘laxi.zip‘, ‘r‘)
z.extractall()
z.close()
--------------------------------------
# 压缩
tar = tarfile.open(‘your.tar‘,‘w‘)
tar.add(‘/Users/wupeiqi/PycharmProjects/bbs2.zip‘, arcname=‘bbs2.zip‘)
tar.add(‘/Users/wupeiqi/PycharmProjects/cmdb.zip‘, arcname=‘cmdb.zip‘)
tar.close()

# 解压
tar = tarfile.open(‘your.tar‘,‘r‘)
tar.extractall()  # 可设置解压地址
tar.close()
-------------------------------------
# shutil.copyfile("time-datetime.py","timeeme")  #拷贝文件
# shutil.copy2("time-datetime.py","time_bak.txt") #拷贝文件和状态信息
# shutil.copytree(r"D:\python培训\our_python\day3",‘day3_bak‘) #递归的去拷贝文件
# shutil.rmtree(‘day3_bak‘) #递归的去删除文件
# shutil.move(‘day3_bak‘,‘day3_bak2‘) #移动文件

1.4 shelve模块

shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式

保存数据:

import shelve
d = shelve.open(‘shelve_test‘)

#存函数
def stu_date(name,age):
print("std",name,age)
#列表
name = [‘zs‘,‘ls‘,‘we‘]
#字典
data = {‘name‘:‘zs‘,‘age‘:‘22‘}

#存储
d[‘func‘] = stu_date
d[‘lis‘] = name
d[‘dic‘] = data

读数据:

import  shelve
def stu_date(name,age):
print("std",name,age)

f = shelve.open(‘shelve_test‘)

print(f[‘func‘](‘zhangsong‘,23))
print(f[‘lis‘])
print(f[‘dic‘])

1.5 XML模块

创建XML文件:

import xml.etree.ElementTree as ET

new_xml = ET.Element("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 = ‘33‘

name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
age = ET.SubElement(name2, "age")
age.text = ‘19‘

et = ET.ElementTree(new_xml) # 生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True)

ET.dump(new_xml) # 打印生成的格式

删除XML文件:

import xml.etree.ElementTree as ET

tree = ET.parse("xml_test.xml")
root = tree.getroot()

for country in root.findall(‘country‘):
rank = int(country.find(‘rank‘).text)
if rank > 50:
root.remove(country)

tree.write(‘output.xml‘)

修改XML文件:

import xml.etree.ElementTree as ET

tree = ET.parse("xml_test_bak.xml")
root = tree.getroot()


# 修改 先将文件全部读入内存,然后采用循环逐条取出数据,修改数据后将数据另存为其他文件或者存回原文件
for node in root.iter(‘year‘):
# print(node,type(node))
# print(node,type(node.text))
new_year = int(node.text) + 1
node.text = str(new_year)
node.set("updated", "yes")

tree.write("xml_test_bak.xml")

读XML文件:

import xml.etree.ElementTree as ET

tree = ET.parse("xml_test.xml")
root = tree.getroot()
print(root.tag)

# 遍历xml文档
# for child in root:
# print(child.tag, child.attrib)
# for i in child:
# print(‘\t‘,i.tag, i.text)
#

# 遍历xml文档中的year
# for child in root:
# #print(child.tag, child.attrib)
# for i in child.iter(‘year‘):
# print(‘\t‘,i.tag, i.text)

# # 只遍历year 节点
# for node in root.iter(‘year‘):
# print(node.tag, node.text)

1.6 ConfigParser模块

创建文件:

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {‘ServerAliveInterval‘: ‘45‘,
‘Compression‘: ‘yes‘,
‘CompressionLevel‘: ‘9‘}

config[‘bitbucket.org‘] = {}
config[‘bitbucket.org‘][‘User‘] = ‘hg‘
config[‘topsecret.server.com‘] = {}
topsecret = config[‘topsecret.server.com‘]
topsecret[‘Host Port‘] = ‘50022‘ # mutates the parser
topsecret[‘ForwardX11‘] = ‘no‘ # same here
config[‘DEFAULT‘][‘ForwardX11‘] = ‘yes‘
with open(‘config.ini‘, ‘w‘) as configfile:
config.write(configfile)

读文件:

import configparser
config = configparser.ConfigParser()
# print(config.sections())
config.read(‘config.ini‘)
print(config.sections())
# print(‘bitbucket.org‘ in config)
print(config[‘bitbucket.org‘][‘User‘])
print(config[‘DEFAULT‘][‘Compression‘])
topsecret = config[‘topsecret.server.com‘]
print(topsecret[‘host port‘])

print("循环".center(20,‘-‘))
for k in config[‘bitbucket.org‘]: #读取指定域和DEFAULT域的key
print(k)

文件操作:

import configparser

config = configparser.ConfigParser()
config.read(‘config.ini‘)

# ########## 读 ##########
# secs = config.sections()
# print secs
# options = config.options(‘group2‘)
# print options

# item_list = config.items(‘group2‘)
# print item_list

# val = config.get(‘group1‘,‘key‘)
# val = config.getint(‘group1‘,‘key‘)

# ########## 改写 ##########
# sec = config.remove_section(‘group1‘)
# config.write(open(‘i.cfg‘, "w"))

# sec = config.has_section(‘wupeiqi‘)
# sec = config.add_section(‘wupeiqi‘)
# config.write(open(‘i.cfg‘, "w"))


# config.set(‘group2‘,‘k1‘,11111)
# config.write(open(‘i.cfg‘, "w"))

# config.remove_option(‘group2‘,‘age‘)
# config.write(open(‘i.cfg‘, "w"))

1.7 hashlib模块

import hashlib

m = hashlib.md5()
m.update(b"Hello")
m.update(b"It‘s me")
#print(m.digest())
m.update(b"It‘s been a long time since last time we ...")

#print(m.digest()) # 2进制格式hash
#print(len(m.hexdigest())) # 16进制格式hash
‘‘‘
def digest(self, *args, **kwargs): # real signature unknown
""" Return the digest value as a string of binary data. """
pass

def hexdigest(self, *args, **kwargs): # real signature unknown
""" Return the digest value as a string of hexadecimal digits. """
pass

‘‘‘
import hashlib

# ######## md5 ########

hash = hashlib.md5()
hash.update(b‘admin‘)
print("md5")
print(hash.hexdigest())

# ######## sha1 ########

hash = hashlib.sha1()
hash.update(b‘admin‘)
print("sh1")
print(hash.hexdigest())

# ######## sha256 ########

hash = hashlib.sha256()
hash.update(b‘admin‘)
print("sh256")
print(hash.hexdigest())

# ######## sha384 ########

hash = hashlib.sha384()
hash.update(b‘admin‘)
print("sh384")
print(hash.hexdigest())

# ######## sha512 ########

hash = hashlib.sha512()
hash.update(b‘admin‘)
print("sh512")
print(hash.hexdigest())

1.8 logging模块

指定日志级别:

import logging

#日志输出到文件并指定日志级别
#logging.basicConfig(filename=‘test.log‘,level=logging.INFO)

#为日志加上时间
logging.basicConfig(format=‘%(asctime)s %(filename)s-%(lineno)d %(levelname)s: %(message)s‘, datefmt=‘%m/%d/%Y %I:%M:%S %p‘)

logging.debug("user [alex] attempted wrong password more than 1 times")
logging.info("user [alex] attempted wrong password more than 2 times")
logging.warning("user [alex] attempted wrong password more than 3 times")
logging.critical("server is down")

不同级别的日志输出:

import logging

# create logger
logger = logging.getLogger(‘TEST-LOG‘)
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)

# create file handler and set level to warning
fh = logging.FileHandler("access.log")
fh.setLevel(logging.ERROR)
# create formatter
ch_formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)
fh_formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)

# add formatter to ch and fh
ch.setFormatter(ch_formatter)
fh.setFormatter(fh_formatter)

# add ch and fh to logger
logger.addHandler(ch)
logger.addHandler(fh)

# ‘application‘ code
logger.debug(‘debug message‘)
logger.info(‘info message‘)
logger.warn(‘warn message‘)
logger.error(‘error message‘)
logger.critical(‘critical message‘)

 






















































































































































































































































































































以上是关于Python基础篇-day6的主要内容,如果未能解决你的问题,请参考以下文章

秋招冲刺应届生JAVA岗-每日5道高频面试题Day6- 基础篇

Python之路,day6-Python基础

Python面向对象基础-day6

python之基础篇——模块与包

python基础学习日志day6-类的继承

Python学习之路-python基础课 (day6)