Python常用的标准库 多线程 第三方库

一、OS模块
os模块主要是对文件,目录的操作
常用方法:
os.mkdir() 创建目录
os.removedirs() 删除文件
os.getcwd() 获取当前目录
os.path.exists(dir or file) 判断文件或者目录是否存在

二、time模块
time.asctime() 国外的时间格式
time.time() 时间戳
time.sleep() 时间等待
time.localtime 时间戳转换成时间元组
time.strftime() 将当前的时间戳转成带格式的时间
格式:time.strftime("%Y-%m-%d%H-%M-%S",time.local)

urllib 库
import urllib.request

math库
math.ceil(x) 返回大于等于参数x的最小整数
math.floor(x) 返回小于等于参数x的最大整数
math.sqrt(x) 平方根

python多线程

一、两种线程的管理
_thread:提供了基本的线程和锁
threading:提供了更高级别、功能更全面的线程管理
1、支持同步机制
2、支持支持守护线程

import threading
import logging
from time import sleep,ctime
logging.basicConfig(level=logging.INFO)
loops=[2,4]
# 生成自己的线程,重写run方法
class MyThread(threading.Thread):
    def __init__(self,func,args):
        threading.Thread.__init__(self)
        self.func = func
        self.args =args

    def run(self) -> None:
        self.func(*self.args)  # 这里解构args参数,他的传入是元组

def loop(nloop,nsec):
    logging.info("start loop " + str(nloop) + " at" + ctime())
    sleep(nsec)
    logging.info("end loop " + str(nloop) + " at" + ctime())

def main():
    logging.info("start all at " + ctime())
    threads = []
    nloops = range(len(loops))

    # 添加线程
    for i in nloops:
        t = MyThread(loop,(i,loops[i]))
        threads.append(t)
    # 执行线程
    for i in nloops:
        threads[i].start()
    # join方法起到监听的作用,他会监听每个threads是否执行完,如果没有,就会阻塞main()不执行完
    # 为什么要阻塞?因为main是主线程,如果main直接执行完,就会杀死所有的字线程。
    for i in nloops:
        threads[i].join()

    logging.info("end all at " + ctime())

if __name__ == "__main__":
    main()

第三方库

pytest
安装:pip install pytest 如果只安装到自己的虚拟环境,先切换到对应的虚拟环境

requests
安装: pip install requests
requests库使用说明:https://requests.readthedocs.io/zh_CN/latest/

外部数据源文件处理(数据驱动)

YAML 一个可读性高,用来表达数据序列化的格式常常作为配置文件使用
JSON 一个轻量级的数据交换语言,该语言以易于让人阅读的文字为基础,用来传输由属性值,或者序列性的值组成的数据对象。
EXCEL 有一个直观的界面,出色的计算功能和图表工具一款电子制表软件

EXCEL
可以驱动EXCEL的库:http://www.python-excel.org/
我们使用的openpyxl库的说明(直接进入Performance看示例):https://openpyxl.readthedocs.io/en/stable/performance.html#
读数据

from openpyxl import load_workbook
wb = load_workbook(filename='large_file.xlsx', read_only=True)
ws = wb['big_data']

for row in ws.rows:
    for cell in row:
        print(cell.value)

# Close the workbook after reading
wb.close()

写数据

from openpyxl import Workbook
wb = Workbook(write_only=True)
ws = wb.create_sheet()
# now we'll fill it with 100 rows x 200 columns
for irow in range(100):
     ws.append(['%d' % i for i in range(200)])
# save the file
wb.save('new_big_file.xlsx') # doctest: +SKIP

JSON读写
JSON使用说明:https://docs.python.org/zh-cn/3/library/json.html
PyYAML
PyYAML使用说明:https://pyyaml.org/wiki/PyYAMLDocumentation

import yaml
# print(yaml.load("""
#   - Hesperiidae
#   - Papilionidae
#   - Apatelodidae
#   - Epiplemidae
#   """, Loader=yaml.FullLoader))

print(yaml.load(open("demo.yml"), Loader=yaml.FullLoader))   #  读取对应的文件数据

demo.yml 文件 嵌套使用上下缩进,对于生成的字典,Value前要有空格。

-
  - AAAAA
  - BBBBB
-
  - CCCC
  - DDDD
  - E: GGG

把字符串转成yaml文件

# 把字符串转成yaml文件
# print(yaml.dump([['AAAAA', 'BBBBB'], ['CCCC', 'DDDD', {'E': 'GGG'}]]))
# 把字符串写入yaml的文件中
with open("demo1.yml","w") as f:
    yaml.dump(data=[['AAAAA', 'BBBBB'], ['CCCC', 'DDDD', {'E': 'GGG'}]],stream=f)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值