文章目录
- numpy(数据处理和科学计算)
- pandas(数据处理和分析)
- matplotlib(数据可视化)
- scikit-learn(机器学习工具)
- tensorflow(深度学习框架)
- keras(深度学习框架)
- requests(HTTP 库)
- flask(Web 框架)
- scrapy(网络爬虫框架)
- beautifulsoup(HTML 解析器)
- selenium(Web 自动化测试)
- ctypes(调用 C 语言库)
- wxPython(GUI 开发)
- pillow(图像处理)
- openpyxl(处理 Excel 文件)
- nltk(自然语言处理)
- jieba(中文分词)
- re(正则表达式)
- datetime(日期时间处理)
- random(随机数生成)
Python 最常用的 20 个包和代码示例(按照使用频率排序):
numpy(数据处理和科学计算)
代码示例:
arr = np.array([1, 2, 3, 4, 5])
print(arr)
pandas(数据处理和分析)
代码示例:
data = {'name': ['John', 'Bob', 'Alice'], 'age': [20, 35, 25]}
df = pd.DataFrame(data)
print(df)
matplotlib(数据可视化)
代码示例:
import matplotlib.pyplot as pltx = [1, 2, 3, 4, 5]
y = [4, 2, 7, 5, 9]
plt.plot(x, y)
plt.show()
scikit-learn(机器学习工具)
代码示例:
from sklearn.linear_model import LinearRegressionX = [[1, 4], [2, 5], [3, 6]]
y = [8, 10, 12]
model = LinearRegression().fit(X, y)
print(model.predict([[4, 7]]))
tensorflow(深度学习框架)
代码示例:
import tensorflow as tfx = tf.constant([1, 2, 3, 4])
y = tf.constant([5, 6, 7, 8])
z = tf.add(x, y)
sess = tf.Session()
print(sess.run(z))
keras(深度学习框架)
代码示例:
from keras.models import Sequential
from keras.layers import Densemodel = Sequential()
model.add(Dense(10, input_dim=5, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')
requests(HTTP 库)
代码示例:
import requestsresponse = requests.get('https://www.baidu.com')
print(response.text)
flask(Web 框架)
代码示例:
from flask import Flask, render_templateapp = Flask(**name**)@app.route('/')
def index():
return render_template('index.html')if **name** == '**main**':
app.run(debug=True)
scrapy(网络爬虫框架)
代码示例:
import scrapyclass MySpider(scrapy.Spider):
name = 'myspider'
start_urls = ['http://quotes.toscrape.com']def parse(self, response):for quote in response.css('div.quote'):yield {'text': quote.css('span.text::text').get(),'author': quote.css('span small::text').get()}
beautifulsoup(HTML 解析器)
代码示例:
from bs4 import BeautifulSouphtml = '<html><head><title>这是标题</title></head><body><p>这是一个段落。</p ></body></html>'
soup = BeautifulSoup(html, 'html.parser')
print(soup.title.text)
selenium(Web 自动化测试)
代码示例:
from selenium import webdriverdriver = webdriver.Chrome()
driver.get('https://www.baidu.com')
search_box = driver.find_element_by_name('wd')
search_box.send_keys('Python')
search_box.submit()
ctypes(调用 C 语言库)
代码示例:
import ctypeslib = ctypes.cdll.LoadLibrary('libexample.so')
lib.add(1, 2)
wxPython(GUI 开发)
代码示例:
import wxapp = wx.App()
frame = wx.Frame(None, title='Hello, wxPython!')
frame.Show()
app.MainLoop()
pillow(图像处理)
代码示例:
from PIL import Imageim = Image.open('test.jpg')
im.show()
openpyxl(处理 Excel 文件)
代码示例:
import openpyxlwb = openpyxl.load_workbook('example.xlsx')
sheet = wb['Sheet1']
cell = sheet['A1']
print(cell.value)
nltk(自然语言处理)
代码示例:
import nltk
sent = ‘This is a sentence.’
tokens = nltk.word_tokenize(sent)
print(tokens)
jieba(中文分词)
代码示例:
import jiebatext = '我爱中文分词'
words = jieba.cut(text)
for word in words:
print(word)
re(正则表达式)
代码示例:
import retext = 'The quick brown fox jumps over the lazy dog.'
pattern = re.compile('fox')
print(pattern.findall(text))
datetime(日期时间处理)
代码示例:
import datetimedt = datetime.datetime.now()
print(dt)
random(随机数生成)
代码示例:
import random
print(random.randint(1, 10))