Python从入门到入土的90行代码之内置包库

Python   2023-03-15 01:46   615   0  

61、 time.time()


当然前提是要导入import time,这其实是个很常用的函数,以时间戳的形式返回当前的时间。


>>> import time
>>> time.time()
1634558595.5172253


62、 calendar.prmonth(2021,10)


可打印日历。。。


>>> import calendar
>>> calendar.prmonth(2021,10)
    October 2021
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
>>>


63 、os.listdir(r"c:\Windows")


返回文件列表,其中r字符串中的\不用于转义。


>>> import os
>>> os.listdir(r"c:\Windows")
['addins``appcompat``apppatch``AppReadiness``assembly``bcastdvr``bfsvc.exe', ...


64 、os.path.join(r"C:\Windows", "python.exe")


合并路径,自动处理\\。


>>> os.path.join(r"C:\Windows", "python.exe")
'C:\\Windows\\python.exe'


65、 glob.glob(r"c:\Windows\*.ini")


可通过通配符返回文件列表。


>>> import glob
>>> glob.glob(r"c:\Windows\*.exe")
['c:\\Windows\\bfsvc.exe``c:\\Windows\\explorer.exe``c:\\Windows\\HelpPane.exe``c:\\Windows\\hh.exe``c:\\Windows\\notepad.exe``c:\\Windows\\py.exe``c:\\Windows\\pyw.exe``c:\\Windows\\regedit.exe``c:\\Windows\\splwow64.exe``c:\\Windows\\Wiainst64.exe``c:\\Windows\\winhlp32.exe``c:\\Windows\\write.exe']
>>>


66-67 、urllib


res = urllib.request.urlopen('https://blog.csdn.net/')
html = res.read()

urllib是python内置的http解析请求库,是大多数爬虫学习者接触的第一个工具。


其中,read()用于读取网页数据,当然,得到的网页数据是未解码数据。


import urllib
res = urllib.request.urlopen('https://blog.csdn.net/')
html = res.read()


68-69 、正则表达式re


content = html.decode('utf-8')
cn = re.findall(r"[\u4e00-\u9fa5]+", content)


此为正则表达式的简单应用,re.findall表示从字符串content中筛选出符合r"[\u4e00-\u9fa5]+"要求的值。所以第一步,是通过utf-8对content进行解码。


而在utf-8中,汉字的序号为\u4e00-\u9fa5;在正则表达式中,[]表示符合条件的集合,+表示出现任意多个符合条件的字符。


>>> import re
>>> content = html.decode('utf-8')
>>> cn = re.findall(r"[\u4e00-\u9fa5]+", content)
>>> cn[:20]
['博客``专业``技术发表平台``博客为中国软件开发者``从业人员``初学者打造交流的专业``技术发表平台``全心致力于帮助开发者通过互联网分享知识``让更多开发者从中受益``一同和``开发者用代码改变未来``头部``广告``频道首页右侧``打底``头部``广告``题目征集``你出我答``做']
>>>


70 、Thread(target=print,args=["hello thread"]).start()


这是最简单的多线程程序,target是将要调用的函数,args为参数列表。


>>> from threading import Thread
>>> Thread(target=print,args=["hello thread"]).start()
hello thread


71-73 、线程池


concurrent.futures中的ThreadPoolExecutor可快速创建线程池,其输入参数为线程池中可以容纳的最多线程数,


from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=5) as ex:
    for i in range(15):
        ex.submit(print,f"{i}")


74-76 进程池


创建单个进程的方法和创建多个线程的方法并无区别,只是把threading中的Thread换成multiprocessing中的Process而已,故不赘言了。


from multiprocessing import Pool
def over(cb):
    print("完成")
p = Pool(5)
for i in range(15):
    p.apply_async(func=print, 
        args = [f"{i}"], callback=over)

77-78、 创建窗口程序tkinter

frame = tkinter.Tk()
frame.mainloop()


其中frame即为tkinter创建的窗口,而mainloop表示进入窗口的消息循环。


>>> import tkinter
>>> frame = tkinter.Tk()
>>> frame.mainloop()


79-80 、通过ctypes调用C语言动态链接库

>>> import ctypes
>>> libc = ctypes.CDLL("msvcrt")
>>> libc.printf("%s".encode(), "Hello ctypes\n".encode())
Hello ctypes
13      #此为该函数的返回值


博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。
闲言碎语
谁说你没有真爱,烦恼与你同在。
赞赏支持

如果觉得博客文章对您有帮助,异或土豪有钱任性,可以通过以下扫码向我捐助。也可以动动手指,帮我分享和传播。您的肯定,是我不懈努力的动力!感谢各位亲~