Python第三方库 —— pyinstaller、jieba、wordcloud、pipenv、tabulate、QRcode

 
 
 
 
 
 
 
 

pyinstaller


cmd调用命令:pyinstaller -F 文件.py

 
 

pyinstaller的参数

参数描述
-h / --help显示帮助信息
-v / --version显示版本信息
–distpath DIR指定输出的目录,默认是./dis
–workpath WDIR指定存储临时文件的目录,默认是./build
-y / --noconfirm强制替换输出目录的内容,没有确认询问
-D / --onedir创建一个目录,包含可执行程序及其依赖文件
-F / --onefile创建一个独立的可执行文件
-n NAME / --name NAME指定输出文件的名称
-i 图标文件.ico指定打包程序使用的图标文件
-w / --windowed, --noconsolepython文件运行时,不打开控制台

pyinstaller默认使用-D,会将程序打包为"执行程序" + "依赖文件"的形式,使用时要把整个文件都复制到目标计算机上才能运行。或者使用-F,只生成一个大的可执行文件

 
 
 
 

使用实例

被打包的程序只有三行内容,一个简单的求平方程序,下面所有例子都会使用此代码
在这里插入图片描述

 
 

打包成一个独立exe程序

cmd 中命令为

pyinstaller --onefile square.py

 

生成结果如下,可执行程序在 dist 目录中。此处的 build 目录可以删除,不影响使用。
在这里插入图片描述
dist 中的 exe 程序可以直接复制到任意地方,并且双击直接执行
在这里插入图片描述
在这里插入图片描述

 

注:该文件有 7.43 MB 大
在这里插入图片描述

 
 

打包成一个小exe程序

打包成小 exe 程序,则代表其依赖并不会一同打包到 exe 中,而是独立储存
在这里插入图片描述

 

注:该 exe 文件仅有 1.96 MB 大

 

使用时需要将 exe 程序及其依赖一同复制到新机器上,并且相对位置不可改变。若想将该 exe 及其依赖打包为一个安装程序,则可借助下方的 NSIS 程序进行。

 
 

使用 NSIS 打包为安装程序

NSIS下载地址

在这里插入图片描述

 

主程序界面如下

在这里插入图片描述

 

将 zip 打包为安装程序

1、将 exe 及其依赖所在的目录打包为 zip 文件
在这里插入图片描述

 

2、使用 NSIS 进行打包

在这里插入图片描述

选择文件冰调整设置
在这里插入图片描述

等待打包完成
在这里插入图片描述
在这里插入图片描述

双击新生成 exe 安装程序,进行安装
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

安装成功,双击执行
在这里插入图片描述
在这里插入图片描述

 
 
 
 
 
 
 
 

pyinstaller 打包注意事项


 

打包 pyecharts

直接使用常用的打包方式,打包出来的exe会无法运行,提示缺少 xxx.json 或者 xxx.html。这是因为 pyecharts 在创建 html 的时候需要 xxx.json 中的预设数据和 xxx.html 的模板
 
解决方式为将数据和模板一起打包进去,代码正常写,打包命令如下:

pyinstaller -F --distpath ./release --workpath ./release/build -i img/hispatial.ico --add-data="D:/codes/venv/hs_sys_monitor_charts/Lib/site-packages/pyecharts/datasets;pyecharts/datasets" --add-data="D:/codes/venv/hs_sys_monitor_charts/Lib/site-packages/pyecharts/render/templates;pyecharts/render/templates" -n hs_system_monitor_charts monitor_charts_cli.py

 
 
 
 
 
 
 
 


jieba

jieba库提供中文分词的功能

 
 

分词模式

jieba库有三种分词模式:
1、精确模式:将文本精确切分开,分词后无冗余,适合文本分词

2、全模式:把文本中所有可能成词的词语都扫描出来,速度快,不能解决歧义

3、搜索引擎模式:在精确模式基础上,对长词再次切分,提高召回率

 
 

常用函数

函数说明返回值
jieba.cut(s)精确模式分词迭代器
jieba.lcut(s)精确模式分词列表
jieba.cut(s, cut_all=True)全模式分词迭代器
jieba.lcut(s, cut_all=True)全模式分词列表
jieba.cut_for_search(s)搜索引擎模式分词迭代器
jieba.lcut_for_search(s)搜索引擎模式分词列表
jieba.add_word(w)向分词的词典添加新词如:jieba.add_word(“蟒蛇语言”)

例如:
1、精确模式分词

import jieba

res = jieba.cut("西北农林科技大学")
print(res)
print(type(res))

在这里插入图片描述

import jieba

res = jieba.lcut("人民英雄永垂不朽")
print(res)
print(type(res))

在这里插入图片描述

2、全模式分词

import jieba

res = jieba.lcut("人民英雄永垂不朽", cut_all=True)
print(res)
print(type(res))

在这里插入图片描述

3、搜索引擎模式

import jieba

res = jieba.lcut_for_search("人民英雄永垂不朽")
print(res)
print(type(res))

在这里插入图片描述

4、添加新词

添加新词前

import jieba

res = jieba.lcut_for_search("python也被称作蟒蛇语言")
print(res)
print(type(res))

在这里插入图片描述
添加新词后

import jieba

jieba.add_word("蟒蛇语言")
res = jieba.lcut_for_search("python也被称作蟒蛇语言")
print(res)
print(type(res))

在这里插入图片描述

 
 
 
 


wordcloud

 
 

简述

一个面向对象的库,实例化词云对象后调用相关属性及方法进行操作。

词云的基本特点:
1、词语中间以空格分隔
2、字体根据词语自行推算
3、默认输出图片大小为 400*200

存在的问题:中文不以空格作为词语的分隔,所以直接将中文字符串输入无法得到有效的输出。此时需要借助jieba库对中文语句进行语义的拆分,再将结果输入词云当中。

 
 

主要方法

方法描述
wordcloud.WordCloud([para])实例化词云对象
w.generate(txt)向词云对象中添加文本
w.to_file(fileName)将词云输出为图片(png / jpg)

w — wordcloudObject

para:

  • width / height —— 控制输出图片的宽、高( wordcloud.WordCloud(width=800, height=600) )
  • min_font_size / max_font_size —— 控制词云中字体的最小(默认为4)、最大字号(根据高度自动调节)( wordcloud.WordCloud(min_font_size=8, max_font_size=10) )
  • font_step —— 词云中字体、字号的步进间距,默认1
  • font_path —— 字体的路径,默认None。如果要显示中文,需要添加此项,如 font_path="msyh.ttc"
  • max_words —— 词云中显示的最大单词数,默认200
  • stop_words —— 不计入统计的词语集合( wordcloud.WordCloud(stop_words={“qwer”}) )
  • mask —— 词云的形状,需要引用imread()函数,默认为长方形。
from scipy.misc import imread
# 背景为白色
mk = imread(pic.png)
w = wordcloud.WordCloud(mask=mk)
  • background_color —— 词云背景颜色,默认为黑色( …(background_color=“white”) )

 

如:

import wordcloud

# 实例化对象
w = wordcloud.WordCloud()

# 添加文本信息,生成词云
w.generate("CHONGQING, Aug. 19 (Xinhua) -- Leading car manufacturers, research institutes, college teams, and individual participants are taking part in a self-driving vehicle challenge game in China, which started Thursday in southwest China's Chongqing Municipality. The four-day game, officially named the i-VISTA Autonomous Vehicle Grand Challenge, consists of five rounds of competitions on environment identification, decision analysis, and the control and execution capability of the vehicles. The highlight of the challenge will be the Advanced Driving Assistance System (ADAS) race, which invites consumers to bring their own vehicles to challenge each other in terms of the vehicles' automatic emergency braking system and automatic parking system. According to the organizer, nearly 30 vehicle models covering almost all the major intelligent automobiles on the market will likely be involved. As one of the major activities of the Smart China Expo, the challenge has been held three times previously, attracting more than 300 teams. The rules are adjusted this year so that consumers can participate with their own cars, said the organizer. The 2021 Smart China Expo, set to be held from Aug. 23 to 25 in Chongqing, aims to promote exchanges in smart technologies and international cooperation in the smart industry. ")

# 输出图片
w.to_file("cw.jpg")

在这里插入图片描述

 
 
 
 

实例 —— 分析《三体》

import wordcloud

# 读入三体
with open("D:/三体.txt", encoding="utf-8") as f:
    txt = f.read()

# 实例化对象
w = wordcloud.WordCloud(font_path="msyh.ttc")

# 添加文本信息,生成词云
w.generate(txt)

# 输出图片
w.to_file("D:/cw.jpg")

在这里插入图片描述

 
 

使用jieba对文本进行语义拆分

import wordcloud, jieba

# 读入三体
with open("D:/三体.txt", encoding="utf-8") as f:
    txt = f.read()

# jieba进行拆词
txt = " ".join(jieba.lcut(txt))

# 实例化对象
w = wordcloud.WordCloud(font_path="msyh.ttc")

# 添加文本信息,生成词云
w.generate(txt)

# 输出图片
w.to_file("D:/cw.jpg")

在这里插入图片描述

 
 

去除某些文字

import wordcloud, jieba

# 读入三体
with open("D:/三体.txt", encoding="utf-8") as f:
    txt = f.read()

# jieba进行拆词
txt = " ".join(jieba.lcut(txt))

# 设置要去除的文字
wdSet = {"这时", "现在", "当然", "是的", "所以", "不过", "我们", "他们", "一个", "自己", "现在", "没有", "可能", "知道", "如果", "然后", "只是", "看到", "这个", "什么", "不是", "就是", "已经", "可以", "这样", "这里", "那个", "出现", "你们", "还是", "一样", "这种", "很快", "一切", "东西", "一直", "两个", "同时"}

# 实例化对象
w = wordcloud.WordCloud(font_path="msyh.ttc", stopwords=wdSet)

# 添加文本信息,生成词云
w.generate(txt)

# 输出图片
w.to_file("D:/cw.jpg")

在这里插入图片描述

 
 

设置最大词数

import wordcloud, jieba

# 读入三体
with open("D:/三体.txt", encoding="utf-8") as f:
    txt = f.read()

# jieba进行拆词
txt = " ".join(jieba.lcut(txt))

# 设置要去除的文字
wdSet = {"这时", "现在", "当然", "是的", "所以", "不过", "我们", "他们", "一个", "自己", "现在", "没有", "可能", "知道", "如果", "然后", "只是", "看到", "这个", "什么", "不是", "就是", "已经", "可以", "这样", "这里", "那个", "出现", "你们", "还是", "一样", "这种", "很快", "一切", "东西", "一直", "两个", "同时"}

# 实例化对象
w = wordcloud.WordCloud(font_path="msyh.ttc", stopwords=wdSet, max_words=20)

# 添加文本信息,生成词云
w.generate(txt)

# 输出图片
w.to_file("D:/cw.jpg")

在这里插入图片描述

 
 

设置词云的图形

注意:在scipy的1.4.1中没有 scipy.misc.imread()方法,要安装1.2.1的版本。
原因:在scipy1.3.0之后便舍弃了imread()方法,所以之后的版本都无法通过此方法去读取图形。可以使用替代库imageio进行。

import imageio
img = imageio.imread(myImage)

我的图形是这样的

在这里插入图片描述

import wordcloud, jieba
import scipy.misc


# 读入三体
with open("D:/三体.txt", encoding="utf-8") as f:
    txt = f.read()

# jieba进行拆词
txt = " ".join(jieba.lcut(txt))

# 设置要去除的文字
wdSet = {"这时", "现在", "当然", "是的", "所以", "不过", "我们", "他们", "一个", "自己", "现在", "没有", "可能", "知道", "如果", "然后", "只是", "看到", "这个", "什么", "不是", "就是", "已经", "可以", "这样", "这里", "那个", "出现", "你们", "还是", "一样", "这种", "很快", "一切", "东西", "一直", "两个", "同时"}

# 读取形状图形
mask = scipy.misc.imread("D:/pic.png")

# 实例化对象
w = wordcloud.WordCloud(font_path="msyh.ttc", stopwords=wdSet, max_words=20, mask=mask)

# 添加文本信息,生成词云
w.generate(txt)

# 输出图片
w.to_file("D:/cw.jpg")

在这里插入图片描述

解除最大词数限制后,图形如下
在这里插入图片描述

 
 
 
 


pipenv

提供隔离的开发环境,基于virtualenv和pip

 
 

pipenv管理结构

通过创建 Pipfile 和 Pipfile.lock 来对虚拟环境进行管理

  • Pip —— 记录虚拟环境信息,包括各类库,仅记录库 不记录版本
  • Pipfile.lock —— 锁定python版本,还记录库的版本

 
 

pipenv使用

 

查看帮助 —— pipenv -h

cmd —> pipenv -h 显示帮助信息

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

 

查看当前目录虚拟环境 —— pipenv --venv

查看当前目录下存在的虚拟环境
在这里插入图片描述

 

创建虚拟环境 —— pipenv --three / pipenv --python 3.6

在这里插入图片描述

在这里插入图片描述

 

进入pipenv的命令行 —— pipenv shell

在这里插入图片描述

 

安装第三方库 —— pipenv install 库

在这里插入图片描述

在这里插入图片描述

 

查看库依赖情况 —— pipenv graph

在这里插入图片描述

 

虚拟环境中调用脚本 —— pipenv run python pyScript.py

只有在虚拟环境的shell中执行代码,才是当前的虚拟环境。退出则按照环境变量自行寻找python解释器

 
 
 
 


tabulate

格式化打印表格数据。支持二维列表、二维迭代类型、字典迭代类型、numpy二维数组、pandas.DataFrame等类型。 且输出风格可以自定义。

如:

import tabulate, pprint

list1 = [["北京", 1], ["上海", 2], ["广州", 3], ["深圳", 4]]

# 普通打印
print(list1)

# pprint打印
pprint.pprint(list1)

# tabulate打印
print(tabulate.tabulate(list1))

在这里插入图片描述

tabulate.tabulate(data, para)

para:

  • headers —— 定义表头
  • tablefmt —— 定义表格风格
  • numalign —— 数字对齐方式

 

headers:

import tabulate

list1 = [["北京", 1], ["上海", 2], ["广州", 3], ["深圳", 4]]

# tabulate打印
print(tabulate.tabulate(list1, headers=["城市", "排名"]))

在这里插入图片描述

 

tablefmt支持的风格

风格
“plain”
“simple”
“grid”
“fancy_grid”
“pipe”
“orgtbl”
“jira”
“presto”
“psql”
“rst”
“rst”
“mediawiki”
“moinmoin”
“youtrack”
“html”
“latex”
“latex_raw”
“latex_booktabs”
“texttile”

 
 
 
 


QRcode

安装 pip install QRcode
使用 import qrcode

支持各种类型数据的二维码生成

使用方法:

img = qrcode.make(txt, border)
  • txt —— 将被转换为二维码的字符换或者字节串等
  • border —— 二维码图形到最外侧的距离,即留白的宽度
  • img —— 返回值,PIL库的image对象

实例:

import qrcode

# 二维码连接
url = "https://www.baidu.com"

# 生成二维码
img = qrcode.make(url)

# 图片保存
img.save("D:/qrImg.png")

在这里插入图片描述

修改border

import qrcode

# 二维码连接
url = "https://www.baidu.com"

# 生成二维码
img = qrcode.make(url, border=1)

# 图片保存
img.save("D:/qrImg.png")

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值