🎈 Participle - jieba
- Excellent Chinese thesaurus, relying on the Chinese thesaurus, using the thesaurus to determine the probability of association between Chinese characters, and forming the word segmentation result
import jieba
word = '伟大的中华人民共和国'
jieba.cut(word)
jieba.lcut(word)
🎈 Word Cloud Library - wordcloud
- Visually highlight an image generated by a high frequency occurrence in the data
关键词
import jieba
import numpy as np
import PIL.Image as Image
from wordcloud import WordCloud
def run(word_path, picture_path):
with open(word_path, 'r') as f:
word = f.read()
cut_word = ' '.join(jieba.cut(word))
color_mask = np.array(Image.open(picture_path))
word_cloud = WordCloud(
# 设置字体,不指定就会出现乱码
font_path='/System/Library/Fonts/PingFang.ttc',
# 设置背景色
background_color='white',
# 词云形状
mask=color_mask,
# 允许最大词汇
max_words=120,
# 最大号字体
max_font_size=2000
).generate(cut_word)
word_cloud.to_file('word_cloud.jpg')
im = word_cloud.to_image()
im.show()
🎈 Visual progress bar - tpdm
- The beautiful progress bar will not only let people know the progress of the task at a glance, but also make you feel happy
from time import sleep
from tqdm import tqdm
# 这里同样的,tqdm就是这个进度条最常用的一个方法
# 里面存一个可迭代对象
for i in tqdm(range(1, 500)):
# 模拟你的任务
sleep(0.01)
sleep(0.5)
🎈 Pretty Table - PrettyTable
- Allows you to print beautiful tables from the command line
import prettytable as pt
# 按行添加数据
tb = pt.PrettyTable()
tb.field_names = ['name', 'age', 'height', 'weight']
tb.add_row(['飞兔', 25, 174, 65])
tb.add_row(['autofelix', 23, 164, 55])
tb.add_row(['极客飞兔', 27, 184, 69.5])
print(tb)
# +-----------+-----+--------+--------+
# | name | age | height | weight |
# +-----------+-----+--------+--------+
# | 飞兔 | 25 | 174 | 65 |
# | autofelix | 23 | 164 | 55 |
# | 极客飞兔 | 27 | 184 | 69.5 |
# +-----------+-----+--------+--------+
🎈 Multiprocessing - multiprocessing
- Create multiple processes
from multiprocessing import Process
def func(s):
print(s)
if __name__ == '__main__':
process = [
Process(target=func, args=('1', ))
Process(target=func, args=('2', ))
]
[p.start() for p in process]
[p.join() for p in process]
🎈 Multithreading - threading
import threading
def func(s):
print(s)
if __name__ == '__main__':
thread = [
threading.Thread(target=func, args=('1', ))
threading.Thread(target=func, args=('2', ))
]
[t.start() for t in thread]
[t.join() for t in thread]
🎈 Google Translate - googletrans
- Automatic language detection, batch translation, language detection, etc.
from googletrans import Translator
translator = Translator()
# 未提供源语言以及翻译的最终语言,会自动翻译成英文
translator.translate('안녕하세요.')
# 告诉它翻译成什么语言
translator.translate('안녕하세요.', dest='ja')
# 告诉它源语言是什么
translator.translate('极客飞兔', src='zh-cn')
# 语言检测
t = ttranslator.detect('이 문장은 한글로 쓰여졌습니다.')
t.lang
🎈 Repeat callback - retrying
- If the request fails, we need to make the request again to prevent data loss due to abnormal request
from retrying import retry
@retry(stop_max_attempt_number=5)
def say():
try:
autofelix
except Exception as e:
# 可以将错误记录日志
print(e)
raise
say()
🎈 Game Development - pygame
- Realize the development of python games, can develop various large and small games
import pygame, sys
from pygame.locals import *
# 初始化pygame
pygame.init()
# 设置窗口的大小,单位为像素
screen = pygame.display.set_mode((500,400), 0, 32)
# 设置窗口的标题
pygame.display.set_caption('用户事件监控')
# 设置背景
screen.fill((255, 255, 255))
# 程序主循环
while True:
# 获取事件
for event in pygame.event.get():
# 判断事件是否为退出事件
if event.type == QUIT:
# 退出pygame
pygame.quit()
# 退出系统
sys.exit()
# 获得键盘按下的事件
if event.type == KEYDOWN:
if(event.key==K_UP or event.key==K_w):
print("上")
if(event.key==K_DOWN or event.key==K_s):
print("下")
if(event.key==K_LEFT or event.key==K_a):
print("左")
if(event.key==K_RIGHT or event.key==K_d):
print("右")
# 按下键盘的Esc键退出
if(event.key==K_ESCAPE):
# 退出pygame
pygame.quit()
# 退出系统
sys.exit()
# 获得鼠标当前的位置
if event.type ==MOUSEMOTION:
print(event.pos)
# 获得鼠标按下的位置
if event.type ==MOUSEBUTTONDOWN:
print("鼠标按下:", event.pos)
# 获得鼠标抬起的位置
if event.type ==MOUSEBUTTONUP:
print("鼠标抬起:", event.pos)
# 绘制屏幕内容
pygame.display.update()
🎈 Drawing Tutorial - turtle
- You can draw all kinds of wonderful patterns, just like the drawing board in the program
from turtle import *
colors = ['red', 'purple', 'blue', 'green', 'yellow', 'orange']
for x in range(360):
pencolor(colors[x % 6])
width(x / 100 + 1)
forward(x)
left(59)
🎈 Data Analysis - pandas
- Data analysis and processing library, created to solve data analysis tasks, functions and methods that can process data quickly and easily
import pandas as pd
info = pd.read_csv("students.csv", encoding = "utf-8")
# 查看数据框的一些属性:最大、最小、均值、四分位数等
info.describe()
# 空值相关的操作
pin = info["pin"]
pin_isnull = pd.isnull(pin)
pin_isnull_list = info[pin_isnull]
len(pin_isnull_list)
# 缺失值相关操作, 简单的处理办法就是过滤掉null值
books = info["life_cycle_books"]
book_isnull = pd.isnull(books)
book_list_isnull = info["life_cycle_books"][book_isnull == False]
mean = sum(book_list_isnull) / len(book_list_isnull)
# 删除缺失值, 所有行
na_info = info.dropna(axis = 1)
# 删除缺失值, 可以指定列
na_info = info.dropna(axis = 0, subset = ["age", "name"])
🎈 Algorithmic encryption - pycryto
- pycryto can implement roughly 3 types of data encryption (one-way encryption, symmetric encryption and asymmetric encryption), generating random numbers, generating key pairs, and digital signatures
from Crypto.Hash import SHA256
hash = SHA256.new()
hash.update('Hello, World!')
# 使用digest()方法加密
digest = hash.digest()
# 使用hexdigest()方法加密,该方法加密后是16进制的
hexdigest = hash.hexdigest()
print(digest, hexdigest)
🎈 Operate win computer - pywin32
- pywin32 wraps the Win32 API of the Windows system and can create and use COM objects and graphical window interfaces
import win32api
import win32con
hid = win32gui.WindowFromPoint((100, 100))
# 获取窗口标题
title = win32gui.GetWindowText(hid)
# 获取窗口类名
class_name = win32gui.GetClassName(hid)
# 模拟鼠标在(400, 500)位置进行点击操作
point = (400, 500)
win32api.SetCursorPos(point)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
🎈 Automated Program Testing - Selenium
- Selenium is a tool for web application testing. Selenium tests run directly in the browser, just like a real user
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 初始化谷歌浏览器
driver = webdriver.Chrome()
# 最大化窗口
driver.maximize_window()
# 打开头条登陆网址
driver.get('https://sso.toutiao.com')
# 等待某个元素是否出现
WebDriverWait(self.driver, 10).until(
EC.text_to_be_present_in_element((By.XPATH, '//*[@id="mobile-code-get"]/span'), u'发送')
)
# 实例化鼠标操作
action = ActionChains(self.driver)
# 按住滑块
action.click_and_hold(self.driver.find_element_by_xpath('//*[@id="captcha_container"]')).perform()
# 将滑块移动x的距离
action.move_by_offset(xoffset=x, yoffset=0).perform()
# 释放滑块
action.release().perform()
🎈 Audio playback - mp3play
- A super small audio manipulation library that can play music and switch between pause and play by pressing the space bar
import mp3play
clip = mp3play.load('music.mp3')
clip.play()
🎈 Web Parsing - BeautifulSoup
- It is a web page parsing library that can quickly analyze the structure of web pages
from bs4 import BeautifulSoup
soup = BeautifulSoup('<p class="name nickname user"><b>i am autofelix</b></p>', 'html.parser')
#获取整个p标签的html代码
print(soup.p)
#获取b标签
print(soup.p.b)
#获取p标签内容,使用NavigableString类中的string、text、get_text()
print(soup.p.text)
#返回一个字典,里面是多有属性和值
print(soup.p.attrs)
#查看返回的数据类型
print(type(soup.p))
#根据属性,获取标签的属性值,返回值为列表
print(soup.p['class'])
#给class属性赋值,此时属性值由列表转换为字符串
soup.p['class']=['Web','Site']
print(soup.p)
🎈 Log processing - logging
import logging
logging.basicConfig(filename='logging.text', level=logging.DEBUG)
logging.debug('It is a debug')
logging.info('It is a info')
logging.warning('It is a warning')
🎈 Image Processing - PIL
- Ideal for image archiving and batch processing of images. You can use PIL to create thumbnails, convert image formats, print images, and more
from PIL import Image
im = Image.open("picture.jpg")
new_im = im.convert('L')
print(new_im.mode)
new_im.show()
🎈 Send Mail - yagmail
- It is a very simple package used to realize the automatic email function, which can send emails to a single person or multiple people at the same time
import yagmail
# 链接邮箱服务器
yag = yagmail.SMTP( user='邮箱地址', password='登录密码', host='smtp.163.com')
# 邮箱正文
contents = ['邮件第一行内容', '邮件第二行内容', '邮件第三行内容']
# 给用户发送邮件并添加多个附件
yag.send(['目标邮箱地址1', '目标邮箱地址2', '目标邮箱地址3'], '邮件标题', contents, ['c://附件.pdf', 'c://picture.jpg'])
🎈 Source packaging - pyinstaller
- Package the source code into an exe file and run it directly on the window
pyinstaller -F -w -p ./lib -i logo.ico main.py
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。