python模块

 
 模块的导入:
 模块可以导入一整个也可以导入其中的部分变量函数类,直接饶了的变脸函数类在使用时候就可以不用再前边写上 模块的名字. 这个部分了,所有需要注意同名函数会覆盖,使用时会调用后引入模块的这个函数
 
包
python package
 
 在后边模块中导入包,需要注意包的路径,这里是包和导包模块在同一级

 主程序运行这一句代码可以阻止导入的包中的全局变量直接在导包模块中执行出来。
 使用:将需要避免打印的部分放在下边图片pass这里

python中常用内置模块

 ①random模块
随机数种子相同产生得随机数也相同

 ②time模块
Python中的时间戳(Timestamp)是指自1970年1月1日(通常称为Unix纪元或Epoch时间)以来经过的秒数(在某些系统中可能包括小数部分,以表示更精确的时间)。这个时间戳是以UTC(协调世界时)为基准的,不依赖于任何特定的时区。时间戳是一种方便的方式,用于在编程中处理时间和日期,因为它允许开发者以数字形式直接比较和计算时间。
在Python中,你可以使用time模块来处理时间戳。例如,你可以使用time.time()函数来获取当前时间的时间戳:
time.time():
import time  
  
# 获取当前时间的时间戳  
timestamp = time.time()  
print(timestamp)  # 输出类似 1622547600.123456 的数字
这个时间戳是一个浮点数,包含了自Epoch以来的秒数以及小数部分(表示秒内的更精确时间)。
此外,Python的datetime模块也提供了与时间戳相关的功能。你可以使用datetime.datetime对象来表示日期和时间,并通过timestamp()方法将其转换为时间戳,或者使用datetime.fromtimestamp()方法将时间戳转换回datetime对象
以下是time模块中的一些函数:

localtime():
import time
op=time.localtime()
print(op)
#time.struct_time(tm_year=2024, tm_mon=8, tm_mday=15, tm_hour=18, tm_min=46, tm_sec=31, tm_wday=3, tm_yday=228, tm_isdst=0)
print('{},{},{}'.format(op.tm_sec,op.tm_mon,op.tm_hour))#31,8,18
time.strftime():
 格式化字符串

formatted_date = time.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_date)#2024-08-15 19:12:52
③datetime模块

 ①需要注意datetime模块中有一个datetime类,可以import datetime from datetime直接引入类使用其中的方法例如now()来获取系统时间
 可以手动给类datetime加入参数设置时间例如dt=datetime(2028,8,8,8,8),这就是2028年的这天,可以直接通过dt.day,dt.year,dt.month取出这些时间,可以直接比较类的对象的大小,即比较对象内的时间
②datetime.timedelta对象表示两个日期或时间之间的差异(例如,天数、秒数之间的差异)。你可以用它来计算两个日期之间的差值,或者将一定的时间量(如天数、小时数等)加到日期或时间上。
from datetime import datetime, timedelta  
  
# 定义两个日期  
date1 = datetime(2023, 4, 1)  
date2 = datetime(2023, 4, 10)  
  
# 计算差异  
difference = date2 - date1  
  
print(difference)  # 输出类似于:9 days, 0:00:00  
  
# 获取天数  
print(difference.days)  # 输出:9**
第三方模块的安装
去cmd里边安装

 例如安装requests库:python -m pip install requests
常见的第三方库:
- requests
  
 获取网页中内容的基本格式:
import requests 
import re  
url = "http:// "
# 使用requests获取网页html内容  
response = requests.get(url)
# 假设所需代码是在html中紧随"Code:"文本之后的字符串  
pattern = r'Code:(\w+)'  
# 使用正则表达式查找所需代码  
matches = re.findall(pattern, response.text)
- openpyxl模块
安装cmd输入pip install openpyxl
 
 可以
 
 也可以
 ①创建一个新的 Excel 文件
from openpyxl import Workbook  
  
# 创建一个Workbook对象,这相当于创建了一个Excel文件  
wb = Workbook()  
  
# 激活worksheet  
ws = wb.active  
  
# 给单元格赋值  
ws['A1'] = "Hello"  
ws.append([1, 2, 3])  # 添加一行数据  
  
# 保存文件  
wb.save("sample.xlsx")
②读取 Excel 文件
from openpyxl import load_workbook  
  
# 加载现有的xlsx文件  
wb = load_workbook(filename='sample.xlsx')  
  
# 选择一个worksheet,默认是第一个worksheet  
ws = wb.active  
  
# 读取单元格的值  
print(ws['A1'].value)  
  
# 遍历所有行  
for row in ws.iter_rows(values_only=True):  
    print(row)  
  
# 遍历所有列  
for col in ws.iter_cols(values_only=True):  
    print(col)
其他第三方模块pdfplumber

 ①模块读取pdf内容:
需要安装pip install pdfplumber
读取pdf中的文字
 
②处理矩阵数组的基础模块numpy
安装pip install numpy
 结合matplotlib模块完成函数的制图pip install matplotlib
③pandas安装
cmd输入pip install pandaspandas同样可以完成excel的读取pandas.read_excel(‘xxx.xlsx’)

 ④又一个数据可视化模块pyecharts
模块帮助文档:https://pyecharts.org/#/zh-cn/intro
安装pip install pyecharts
文档中Pie - Pie_base
.add用列表生成式,将打包数据生成为列表,打印[list(z) for z in zip(Faker.choose(), Faker.values())]发现pie图需要二维列表,可以自己找二位数组直接代替进来改代码
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
c = (
    Pie()
    .add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
    .set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    .render("pie_base.html")
)
⑤jieba模块用于人工智能和自然语言的处理
 安装pip install jieba
 
 https://github.com/fxsjy/jieba
 jieba对文本进行分词统计
 jieba.lcut() 得到的数据类型是列表,可以通过变成set集合来去重例如word=set(word1),然后通过字典来完成遍历和计数
 或者
import jieba  
from collections import Counter  
  
# 假设我们有一个文本字符串  
text = "Python是一种广泛使用的高级编程语言。它的设计哲学强调代码的可读性,以及用一种方法,最好是只有一种方法来做一件事。Python的语法允许程序员用更少的代码行来表达想法。"  
  
# 使用jieba进行分词  
words = jieba.lcut(text)  
  
# 使用collections.Counter来统计词频  
word_counts = Counter(words)  
  
# 打印前10个最高频的词  
most_common_words = word_counts.most_common(10)  
for word, count in most_common_words:  
    print(f"{word}: {count}")  
  
# 如果你想要忽略标点符号等非词语,你可能需要在分词前对文本进行预处理  
# 或者使用jieba的filter_token功能(如果你已经加载了自定义词典)  
# 例如,去除长度为1的词语(假设它们都是标点符号或单字符)  
filtered_words = [word for word in words if len(word) > 1]  
filtered_counts = Counter(filtered_words)  
  
# 打印过滤后的前10个最高频的词  
most_common_filtered_words = filtered_counts.most_common(10)  
for word, count in most_common_filtered_words:  
    print(f"{word}: {count}")
⑥pyinstaller打包需要注意需要打包的文件的路径中最好不要包含中文
 
安装pip install pyinstaller,按照图上在cmd输入需要打包的内容,打包后的文件存放的位置会显示在cmd中,大概在c盘



















