CodeBlog

A person blog.

Python 正则表达式完全指南

正则表达式(Regular Expression,简称 Regex)是一种描述字符串匹配规则的“微型语言”,几乎所有的编程语言都支持它。Python 通过内置的 re 模块提供了完整的正则表达式支持,无需安装任何第三方库即可使用。本文将系统介绍 Python 正则表达式的语法、re 模块的常用函数、分组与断言等进阶用法,以及数据清洗、日志解析、表单校验等实战场景。

一、正则表达式基础概念

1. 什么是正则表达式

正则表达式是一串由普通字符和元字符组成的模式串,用来描述一类字符串的匹配规则。例如 \d{11} 表示“连续 11 位数字”,可以用来匹配手机号:

1
2
3
4
5
import re

text = "联系方式:13812345678"
result = re.search(r"\d{11}", text)
print(result.group()) # 13812345678

2. 为什么推荐使用原始字符串

正则表达式中大量使用反斜杠 \,而 Python 字符串中 \ 也是转义符。为了避免二次转义,强烈建议使用原始字符串 r"...":

1
2
3
4
5
6
7
8
9
10
import re

# 不推荐:\d 会被 Python 当作普通转义处理,容易出错
pattern1 = "\d{3}"

# 推荐:原始字符串,所见即所得
pattern2 = r"\d{3}"

print(re.findall(pattern1, "abc123")) # ['123']
print(re.findall(pattern2, "abc123")) # ['123']

3. 一个完整的匹配流程

Python 中使用正则表达式的标准流程如下:

  1. 编写模式串(Pattern),必要时用 r"" 包裹;
  2. 调用 re 模块函数或 re.compile() 编译模式;
  3. 从匹配结果(Match 对象)中提取分组、位置等信息;
  4. 根据业务需要做替换、拆分或校验。

二、re 模块核心函数

re 模块提供了 8 个最常用的函数,下面逐一说明。

1. re.match:从字符串开头匹配

re.match() 只在字符串开头尝试匹配,匹配成功返回 Match 对象,失败返回 None。

1
2
3
4
import re

print(re.match(r"\d+", "123abc")) # <re.Match object; span=(0, 3), match='123'>
print(re.match(r"\d+", "abc123")) # None(开头不是数字)

2. re.search:扫描整个字符串

re.search() 会扫描整个字符串,返回第一个匹配结果。

1
2
3
import re

print(re.search(r"\d+", "abc123def456").group()) # 123

注意:match 与 search 只返回一个结果,二者的区别仅在于是否限定“从开头匹配”。

3. re.fullmatch:完全匹配

re.fullmatch() 要求整个字符串都符合模式,非常适合做格式校验。

1
2
3
4
import re

print(re.fullmatch(r"\d{11}", "13812345678")) # 匹配成功
print(re.fullmatch(r"\d{11}", "138123456789")) # None(长度不符)

4. re.findall:返回所有匹配

re.findall() 返回所有非重叠匹配组成的列表;如果模式中有分组,则返回分组内容。

1
2
3
4
5
import re

text = "订单号:A1001,订单号:B2002"
print(re.findall(r"[A-Z]\d{4}", text)) # ['A1001', 'B2002']
print(re.findall(r"([A-Z])(\d{4})", text)) # [('A', '1001'), ('B', '2002')]

5. re.finditer:返回迭代器

当匹配结果很多时,re.finditer() 返回迭代器,比 findall 更省内存,并且可以获取每个匹配的位置。

1
2
3
4
5
import re

text = "a=1, b=2, c=3"
for m in re.finditer(r"(\w)=(\d)", text):
print(m.group(1), "->", m.group(2), "位置:", m.span())

6. re.sub:替换

re.sub() 用于替换匹配到的内容,第二个参数可以是字符串,也可以是函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
import re

# 简单替换
print(re.sub(r"\d+", "*", "手机号:13812345678")) # 手机号:*

# 使用分组引用
print(re.sub(r"(\d{3})\d{4}(\d{4})", r"\1****\2", "13812345678")) # 138****5678

# 使用函数动态替换
def double(match):
return str(int(match.group()) * 2)

print(re.sub(r"\d+", double, "a=1, b=2")) # a=2, b=4

7. re.split:分割

re.split() 支持用正则作为分隔符,这是 str.split() 做不到的。

1
2
3
import re

print(re.split(r"[,;、\s]+", "苹果, 香蕉;橙子、西瓜")) # ['苹果', '香蕉', '橙子', '西瓜']

8. re.compile:预编译模式

将常用的模式编译成 Pattern 对象,可复用且性能更好。

1
2
3
4
import re

PHONE = re.compile(r"1[3-9]\d{9}")
print(PHONE.findall("联系:13812345678、15987654321"))

三、常用语法速查

1. 字符类

语法 说明
. 匹配除换行符外的任意单个字符
\d 数字,等价于 [0-9]
\D 非数字
\w 字母、数字、下划线(含中文等 Unicode 字母)
\W 非 \w
\s 空白字符(空格、制表符、换行等)
\S 非空白字符
[abc] 匹配 a、b、c 中的任意一个
[^abc] 匹配除 a、b、c 之外的任意字符
[a-z0-9] 匹配范围内的字符
1
2
3
4
import re

print(re.findall(r"\w+", "hello_世界 123")) # ['hello_世界', '123']
print(re.findall(r"[^0-9]+", "a1b2c3")) # ['a', 'b', 'c']

2. 量词

语法 说明
* 0 次或多次
+ 1 次或多次
? 0 次或 1 次
{n} 恰好 n 次
{n,} 至少 n 次
{n,m} n 到 m 次
*? +? ?? 非贪婪模式
1
2
3
4
5
import re

print(re.findall(r"a+", "aaa b aa c a")) # ['aaa', 'aa', 'a']
print(re.findall(r"<.+>", "<b>粗体</b>")) # ['<b>粗体</b>'](贪婪)
print(re.findall(r"<.+?>", "<b>粗体</b>")) # ['<b>', '</b>'](非贪婪)

3. 锚点与边界

语法 说明
^ 字符串开头(多行模式下为行首)
$ 字符串结尾(多行模式下为行尾)
\b 单词边界
\B 非单词边界
\A 字符串绝对开头
\Z 字符串绝对结尾
1
2
3
4
import re

print(re.findall(r"\bcat\b", "cat category cat.")) # ['cat', 'cat']
print(re.findall(r"^\d+", "123\n456", re.M)) # ['123', '456']

4. 分组、选择与断言

语法 说明
(...) 捕获分组
(?:...) 非捕获分组
(?P<name>...) 命名分组
(?P=name) 引用命名分组
| 或,例如 cat|dog
(?=...) 正向先行断言(后面必须是什么)
(?!...) 负向先行断言(后面必须不是什么)
(?<=...) 正向后行断言(前面必须是什么)
(?<!...) 负向后行断言
1
2
3
4
5
6
7
8
9
import re

text = "价格:¥199,原价:¥299"

# 提取 ¥ 后面的数字(先行断言)
print(re.findall(r"(?<=¥)\d+", text)) # ['199', '299']

# 提取数字后面不是“元”的数字
print(re.findall(r"\d+(?!元)", text)) # ['199', '299']

四、分组与捕获进阶

1. 普通分组与编号

分组按左括号出现的顺序从 1 开始编号,group(0) 表示整个匹配。

1
2
3
4
5
6
import re

m = re.search(r"(\d{4})-(\d{2})-(\d{2})", "日期:2026-09-22")
print(m.group(0)) # 2026-09-22
print(m.group(1)) # 2026
print(m.groups()) # ('2026', '09', '22')

2. 命名分组

命名分组可读性更好,groupdict() 可直接得到字典。

1
2
3
4
5
import re

m = re.search(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})", "2026-09-22")
print(m.group("year"), m.group("month"), m.group("day")) # 2026 09 22
print(m.groupdict()) # {'year': '2026', 'month': '09', 'day': '22'}

3. 非捕获分组

只做分组而不需要提取内容时,使用 (?:...) 可以减少内存开销。

1
2
3
4
import re

text = "abcabcabc"
print(re.findall(r"(?:abc)+", text)) # ['abcabcabc']

4. 反向引用

\1、\2 表示引用前面捕获到的内容,常用于查找重复内容。

1
2
3
import re

print(re.findall(r"\b(\w+)\s+\1\b", "hello hello world")) # ['hello']

五、匹配模式(标志位)

re 模块提供多个标志位,可以组合使用(用 | 连接)。

标志位 说明
re.I 忽略大小写
re.M 多行模式,^ $ 匹配行首行尾
re.S 让 . 匹配换行符
re.X 详细模式,允许在模式中加注释和空白
re.A ASCII 模式,\w 等只匹配 ASCII 字符
1
2
3
4
5
6
7
8
9
10
11
import re

print(re.findall(r"python", "Python PYTHON python", re.I)) # ['Python', 'PYTHON', 'python']

# 详细模式让复杂正则更易读
pattern = re.compile(r"""
(?P<year>\d{4}) # 年
-(?P<month>\d{2}) # 月
-(?P<day>\d{2}) # 日
""", re.X)
print(pattern.search("2026-09-22").groupdict())

六、Match 对象常用方法

match、search、fullmatch 返回的都是 Match 对象,常用方法如下:

1
2
3
4
5
6
7
8
9
10
11
import re

m = re.search(r"(?P<num>\d+)", "编号:8888")

print(m.group()) # 8888,整体匹配
print(m.group("num")) # 8888,按名称取值
print(m.groups()) # ('8888',),所有分组
print(m.groupdict()) # {'num': '8888'},命名分组字典
print(m.span()) # (3, 7),起止位置
print(m.start(), m.end()) # 3 7
print(m.string) # 编号:8888,被匹配的原始字符串

七、实战案例

1. 示例 1:表单数据校验

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
import re

def validate(value, pattern, message):
"""通用校验函数:返回 (是否通过, 提示信息)"""
if re.fullmatch(pattern, value):
return True, "校验通过"
return False, message

rules = [
(r"1[3-9]\d{9}", "手机号格式不正确"),
(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", "邮箱格式不正确"),
(r"\d{17}[\dXx]", "身份证号格式不正确"),
(r"(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,20}", "密码需包含大小写字母和数字,长度 8-20 位"),
]

data = [
("13812345678", rules[0]),
("test@example.com", rules[1]),
("11010119900307123X", rules[2]),
("Abc12345", rules[3]),
]

for value, (pattern, message) in data:
ok, msg = validate(value, pattern, message)
print(f"{value:24} -> {'通过' if ok else '失败:' + msg}")

2. 示例 2:解析日志文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import re

LOG_LINE = re.compile(
r"(?P<ip>\d+\.\d+\.\d+\.\d+)\s+-\s+-\s+"
r"\[(?P<time>[^\]]+)\]\s+"
r'"(?P<method>[A-Z]+)\s+(?P<path>\S+)\s+HTTP/[\d.]+"\s+'
r"(?P<status>\d{3})\s+(?P<size>\d+)"
)

log = '''192.168.1.10 - - [22/Sep/2026:10:01:12 +0800] "GET /index.html HTTP/1.1" 200 5321
10.0.0.5 - - [22/Sep/2026:10:01:15 +0800] "POST /api/login HTTP/1.1" 500 128
192.168.1.10 - - [22/Sep/2026:10:02:03 +0800] "GET /images/logo.png HTTP/1.1" 200 20480'''

status_count = {}
for line in log.splitlines():
m = LOG_LINE.match(line)
if not m:
continue
info = m.groupdict()
status_count[info["status"]] = status_count.get(info["status"], 0) + 1
print(f"{info['ip']:15} {info['method']:5} {info['path']:20} {info['status']}")

print("\n状态码统计:", status_count)

3. 示例 3:文本清洗与敏感信息脱敏

1
2
3
4
5
6
7
8
9
10
11
12
13
import re

def clean_text(text):
"""清洗文本:去 HTML 标签、压缩空白、脱敏手机号与邮箱"""
text = re.sub(r"<[^>]+>", "", text) # 去 HTML 标签
text = re.sub(r"\s+", " ", text).strip() # 压缩空白
text = re.sub(r"(1[3-9]\d)\d{4}(\d{4})", r"\1****\2", text) # 手机号脱敏
text = re.sub(r"(\w)[\w.%+-]*(?=@)", r"\1***", text) # 邮箱脱敏
return text

raw = "<p> 联系人:13812345678,邮箱:zhangsan@example.com </p>\n\n"
print(clean_text(raw))
# 联系人:138****5678,邮箱:z***@example.com

4. 示例 4:批量重命名文件

1
2
3
4
5
6
7
8
9
10
11
12
13
import re
from pathlib import Path

# 将 IMG_20260922_101530.jpg 重命名为 2026-09-22_10-15-30.jpg
PATTERN = re.compile(r"IMG_(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})")

def build_new_name(filename):
return PATTERN.sub(r"\1-\2-\3_\4-\5-\6", filename)

for name in ["IMG_20260922_101530.jpg", "IMG_20260922_183001.png"]:
print(f"{name} -> {build_new_name(name)}")
# IMG_20260922_101530.jpg -> 2026-09-22_10-15-30.jpg
# IMG_20260922_183001.png -> 2026-09-22_18-30-01.png

5. 示例 5:提取网页中的关键信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import re
from html import unescape

html = '''
<html><head><title>Python 正则表达式完全指南</title></head>
<body>
<a href="https://www.ttx-sky.top/post/1">文章一</a>
<a href="https://www.ttx-sky.top/post/2">文章二</a>
</body></html>
'''

title = re.search(r"<title>(.*?)</title>", html, re.S).group(1)
links = re.findall(r'<a\s+href="([^"]+)"[^>]*>(.*?)</a>', html, re.S)

print("标题:", unescape(title))
for url, text in links:
print(f"{text} -> {url}")

八、常见错误及解决方案

1. 忘记使用原始字符串

错误现象:re.error: bad escape \d at position 0,或匹配结果与预期不符。

原因:"\d" 经过 Python 字符串转义后可能变成其他字符(例如 "\b" 会变成退格符)。

解决方案:

1
2
3
4
5
6
7
import re

# 错误示例中的问题:\b 被解释成退格符
print(len("\b")) # 1,是退格符而不是反斜杠 + b

# 正确做法:始终使用原始字符串
print(re.findall(r"\bword\b", "a word here")) # ['word']

2. 贪婪匹配导致结果过长

错误现象:<.+> 把整行 HTML 都匹配掉了。

解决方案:使用非贪婪量词 +?、*?,或明确排除目标字符:

1
2
3
4
5
import re

html = "<b>粗体</b><i>斜体</i>"
print(re.findall(r"<.+?>", html)) # ['<b>', '</b>', '<i>', '</i>']
print(re.findall(r"<[^>]+>", html)) # ['<b>', '</b>', '<i>', '</i>'](推荐:无回溯)

3. 特殊字符未转义

错误现象:模式中包含 . * + ( ) [ ] ? ^ $ | \ 等特殊字符时报错或误匹配。

解决方案:使用 re.escape() 自动转义:

1
2
3
4
5
6
import re

keyword = "Python 3.12 (正式版)"
pattern = re.escape(keyword)
print(pattern) # Python\ 3\.12\ \(正式版\)
print(bool(re.search(pattern, "更新:Python 3.12 (正式版) 已发布"))) # True

4. 忘记考虑换行符

错误现象:跨行文本匹配不到。

原因:默认情况下 . 不匹配换行符,^ $ 只匹配整串的开头结尾。

解决方案:加上 re.S 或 re.M 标志:

1
2
3
4
5
import re

text = "第一行\n第二行"
print(re.findall(r"^.+$", text)) # ['第一行', '第二行']
print(re.findall(r"第一行.第二行", text, re.S)) # ['第一行\n第二行']

5. 重复编译模式影响性能

错误现象:在循环中反复调用 re.match 处理海量数据时速度慢。

原因:re 模块内部虽有缓存,但大量不同模式仍会反复编译。

解决方案:将模式提前编译为 Pattern 对象复用:

1
2
3
4
5
6
import re

PATTERN = re.compile(r"1[3-9]\d{9}")

def extract_phones(lines):
return [m for line in lines for m in PATTERN.findall(line)]

6. 灾难性回溯(Catastrophic Backtracking)

错误现象:正则执行时 CPU 满载、程序长时间无响应。

原因:嵌套量词(如 (a+)+b)在长字符串不匹配时会指数级回溯。

解决方案:避免嵌套量词;用更精确的字符类代替 .;必要时限制匹配长度:

1
2
3
4
5
import re

# 危险写法:(a+)+
# 安全写法:明确字符集与数量,避免嵌套量词
print(bool(re.fullmatch(r"a+b", "a" * 20 + "b"))) # False,但瞬间返回

九、最佳实践

  1. 始终使用原始字符串:r"..." 能避免绝大多数莫名其妙的转义问题。

  2. 预编译常用模式:把高频模式提取为模块级 re.compile() 常量。

  3. 优先用字符类代替通配:[^>]+ 比 .+? 更高效,且不会误匹配。

  4. 能用 fullmatch 就不用 match + $:语义更清晰,更适合校验场景。

  5. 解析 HTML/JSON 请用专门库:正则适合简单、结构稳定的文本;复杂 HTML 建议使用 BeautifulSoup,JSON 直接使用 json 模块。

  6. 善用命名分组与 re.X:复杂正则加上命名分组和注释,可读性会明显提升。

  7. 为复杂正则写测试:用 re.fullmatch 对典型样本和边界样本做单元测试,避免线上误判。

  8. 注意 Unicode 行为:Python 3 中 \w、\d 默认支持 Unicode,若只想要 ASCII,请加上 re.A。

  9. 考虑性能上限:对超长文本先用 str.find 做粗筛,再用正则精匹配;必要时设置超时或分块处理。

  10. 校验与提取分离:校验用 fullmatch,提取用 findall/finditer,不要一个模式承担两种职责。

十、总结

本文系统梳理了 Python 正则表达式的核心知识:

  1. 基础语法:字符类、量词、锚点、分组与断言,是写出正确模式的前提;

  2. re 模块函数:match、search、fullmatch、findall、finditer、sub、split、compile 覆盖了绝大多数使用场景;

  3. 进阶技巧:命名分组、非捕获分组、反向引用、先行/后行断言以及标志位组合,可显著提升表达能力和可读性;

  4. 实战场景:表单校验、日志解析、文本清洗、批量重命名、网页信息提取;

  5. 常见陷阱:转义、贪婪匹配、换行处理、灾难性回溯,是排查问题时的重点方向。

正则表达式是一门“投入一次、受用很久”的技能。掌握它之后,处理文本数据、编写校验规则、做日志分析时都会事半功倍。建议在日常开发中逐步积累自己的“正则片段库”,并在使用时始终配合测试,保证规则的准确性。

十一、参考资料

  1. Python 官方文档 - re 模块

  2. Python 官方文档 - 正则表达式 HOWTO

  3. 正则表达式 30 分钟入门教程

  4. MDN - 正则表达式

  5. regex101 - 在线正则测试与调试

  6. RE2 语法参考(性能友好型正则引擎)

pandas 基础使用教程

在数据分析领域,pandas 是 Python 生态中最强大、最常用的库之一。它提供了高效的数据结构和数据分析工具,使得数据处理、清洗、分析和可视化变得简单易用。本文将详细介绍 pandas 的基础使用方法,包括依赖库、基本概念、数据操作、常见错误及解决方案等内容。

一、依赖的库

要使用 pandas 进行数据分析,我们需要安装一些必要的依赖库。

1. pandas

pandas 是核心库,提供了 DataFrame 和 Series 等数据结构,以及各种数据操作功能。

安装方法 :

1
pip install pandas

版本要求 :

  • 推荐使用最新稳定版本

  • 目前稳定版本:v2.2.4(截至 2026 年 1 月)

2. numpy

numpy 是 pandas 的基础依赖,提供了高效的数值计算功能。

安装方法 :

1
pip install numpy

版本要求 :

  • 推荐版本:v2.1.0 或更高

3. matplotlib(可选)

matplotlib 用于数据可视化,pandas 内置了对 matplotlib 的支持。

安装方法 :

1
pip install matplotlib

版本要求 :

  • 推荐版本:v3.9.0 或更高

4. seaborn(可选)

seaborn 是基于 matplotlib 的高级可视化库,提供了更美观的图表样式。

安装方法 :

1
pip install seaborn

版本要求 :

  • 推荐版本:v0.13.2 或更高

二、基本概念

pandas 有两个核心数据结构:Series 和 DataFrame。

1. Series

Series 是一维带标签的数组,类似于带索引的列表。

创建 Series :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import pandas as pd
import numpy as np
# 从列表创建
ser1 = pd.Series([1, 2, 3, 4, 5])
print("从列表创建的Series:")
print(ser1)
print()
# 从字典创建
ser2 = pd.Series({'a': 1, 'b': 2, 'c': 3})
print("从字典创建的Series:")
print(ser2)
print()
# 带自定义索引
ser3 = pd.Series([1, 2, 3], index=['x', 'y', 'z'])
print("带自定义索引的Series:")
print(ser3)

2. DataFrame

DataFrame 是二维带标签的数据结构,类似于电子表格或 SQL 表。

创建 DataFrame :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 从字典创建
df1 = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']
})
print("从字典创建的DataFrame:")
print(df1)
print()
# 从列表创建
data = [
['Alice', 25, 'New York'],
['Bob', 30, 'London'],
['Charlie', 35, 'Paris']
]
df2 = pd.DataFrame(data, columns=['Name', 'Age', 'City'])
print("从列表创建的DataFrame:")
print(df2)
print()
# 从numpy数组创建
data = np.random.randint(0, 100, size=(5, 3))
df3 = pd.DataFrame(data, columns=['A', 'B', 'C'])
print("从numpy数组创建的DataFrame:")
print(df3)

三、数据读取与写入

pandas 支持多种文件格式的数据读取和写入。

1. CSV 文件

读取 CSV 文件 :

1
2
3
4
5
6
7
8
9
10
11
12
13
# 读取本地CSV文件
df = pd.read_csv('data.csv')
# 读取网络CSV文件
df = pd.read_csv('https://example.com/data.csv')
# 读取时指定参数
df = pd.read_csv(
'data.csv',
sep=',', # 分隔符
header=0, # 表头行
index_col=0, # 索引列
na_values=['NA'], # 缺失值标记
encoding='utf-8' # 编码
)

写入 CSV 文件 :

1
2
3
4
5
6
7
8
9
10
# 写入CSV文件
df.to_csv('output.csv')
# 写入时指定参数
df.to_csv(
'output.csv',
index=False, # 不写入索引
header=True, # 写入表头
sep=',', # 分隔符
encoding='utf-8' # 编码
)

2. Excel 文件

读取 Excel 文件 :

1
2
3
4
5
6
# 读取Excel文件
df = pd.read_excel('data.xlsx')
# 读取指定工作表
df = pd.read_excel('data.xlsx', sheet_name='Sheet1')
# 读取多个工作表
dfs = pd.read_excel('data.xlsx', sheet_name=['Sheet1', 'Sheet2'])

写入 Excel 文件 :

1
2
3
4
5
6
7
8
# 写入Excel文件
df.to_excel('output.xlsx')
# 写入指定工作表
df.to_excel('output.xlsx', sheet_name='Sheet1')
# 写入多个工作表
with pd.ExcelWriter('output.xlsx') as writer:
df1.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name='Sheet2')

3. JSON 文件

读取 JSON 文件 :

1
2
3
4
# 读取JSON文件
df = pd.read_json('data.json')
# 读取嵌套JSON
df = pd.read_json('nested.json', orient='records')

写入 JSON 文件 :

1
2
3
4
# 写入JSON文件
df.to_json('output.json')
# 写入时指定格式
df.to_json('output.json', orient='records', indent=2)

四、数据清洗与预处理

数据清洗是数据分析的重要步骤,包括处理缺失值、重复值、异常值等。

1. 处理缺失值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 查看缺失值情况
print(df.isnull().sum())
# 删除包含缺失值的行
df_clean = df.dropna()
# 删除包含缺失值的列
df_clean = df.dropna(axis=1)
# 填充缺失值
df_filled = df.fillna(0) # 用0填充
print(df_filled)
df_filled = df.fillna({'Age': 30, 'City': 'Unknown'}) # 按列填充
print(df_filled)
df_filled = df.fillna(method='ffill') # 向前填充
print(df_filled)
df_filled = df.fillna(method='bfill') # 向后填充
print(df_filled)
df_filled = df.fillna(df.mean()) # 用均值填充
print(df_filled)

2. 处理重复值

1
2
3
4
5
6
7
8
9
10
11
12
13
# 查看重复值
print(df.duplicated())
# 查看重复值数量
print(df.duplicated().sum())
# 删除重复值
df_unique = df.drop_duplicates()
print(df_unique)
# 按指定列删除重复值
df_unique = df.drop_duplicates(subset=['Name'])
print(df_unique)
# 保留最后一个重复值
df_unique = df.drop_duplicates(keep='last')
print(df_unique)

3. 数据类型转换

1
2
3
4
5
6
7
8
9
# 查看数据类型
print(df.dtypes)
# 转换数据类型
df['Age'] = df['Age'].astype(int) # 转换为整数
print(df.dtypes)
df['Date'] = pd.to_datetime(df['Date']) # 转换为日期时间
print(df.dtypes)
df['Price'] = pd.to_numeric(df['Price']) # 转换为数值型
print(df.dtypes)

4. 数据标准化与归一化

1
2
3
4
5
6
7
8
9
10
# 标准化(z-score)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
df['Age_scaled'] = scaler.fit_transform(df[['Age']])
print(df)
# 归一化(min-max)
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
df['Age_normalized'] = scaler.fit_transform(df[['Age']])
print(df)

五、数据筛选与查询

1. 基本筛选

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 选择列
df['Name'] # 选择单列
print(df['Name'])
df[['Name', 'Age']] # 选择多列
print(df[['Name', 'Age']])
# 行索引选择
df.loc[0] # 按标签选择行
print(df.loc[0])
df.loc[0:2] # 按标签切片
print(df.loc[0:2])
df.iloc[0] # 按位置选择行
print(df.iloc[0])
df.iloc[0:2] # 按位置切片
print(df.iloc[0:2])
# 条件筛选
df[df['Age'] > 30] # 年龄大于30的行
print(df[df['Age'] > 30])
df[(df['Age'] > 30) & (df['City'] == 'New York')] # 多条件筛选
print(df[(df['Age'] > 30) & (df['City'] == 'New York')])
df[df['City'].isin(['New York', 'London'])] # 包含于列表
print(df[df['City'].isin(['New York', 'London'])])

2. 高级查询

1
2
3
4
5
6
7
8
# 使用query方法
df.query('Age > 30')
print(df.query('Age > 30'))
df.query('Age > 30 and City == "New York"')
print(df.query('Age > 30 and City == "New York"'))
# 使用eval方法计算新列
df.eval('Age_in_months = Age * 12')
print(df.eval('Age_in_months = Age * 12'))

六、数据分组与聚合

1. 数据分组

1
2
3
4
5
6
7
8
9
10
11
# 按单列分组
grouped = df.groupby('City')
print(grouped)
# 按多列分组
grouped = df.groupby(['City', 'Gender'])
print(grouped)
# 分组后查看每组数据
for name, group in grouped:
print(f"Group: {name}")
print(group)
print()

2. 聚合操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 常用聚合函数
print(grouped['Age'].count()) # 计数
print(grouped['Age'].sum()) # 求和
print(grouped['Age'].mean()) # 均值
print(grouped['Age'].median()) # 中位数
print(grouped['Age'].std()) # 标准差
print(grouped['Age'].min()) # 最小值
print(grouped['Age'].max()) # 最大值
# 多聚合函数
grouped['Age'].agg(['count', 'sum', 'mean', 'min', 'max'])
print(grouped['Age'].agg(['count', 'sum', 'mean', 'min', 'max']))
# 不同列使用不同聚合函数
grouped.agg({
'Age': ['mean', 'std'],
'Salary': ['sum', 'max']
})

3. 分组转换

1
2
3
4
5
6
# 计算每组的均值并广播到原数据
print(grouped['Age'].transform('mean'))
# 使用自定义函数
def normalize(x):
return (x - x.mean()) / x.std()
print(grouped['Age'].transform(normalize))

4. 分组过滤

1
2
# 过滤掉年龄均值小于30的组
print(grouped.filter(lambda x: x['Age'].mean() > 30))

七、数据可视化

pandas 内置了对 matplotlib 的支持,可以直接绘制各种图表。

1. 基本图表

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
import matplotlib.pyplot as plt
# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
# 折线图
df.plot(kind='line', x='Date', y='Value')
plt.title('折线图')
plt.show()
# 柱状图
df.plot(kind='bar', x='Name', y='Age')
plt.title('柱状图')
plt.show()
# 直方图
df['Age'].plot(kind='hist', bins=10)
plt.title('直方图')
plt.show()
# 散点图
df.plot(kind='scatter', x='Age', y='Salary')
plt.title('散点图')
plt.show()
# 箱线图
df.plot(kind='box', y=['Age', 'Salary'])
plt.title('箱线图')
plt.show()
# 饼图
df['City'].value_counts().plot(kind='pie', autopct='%1.1f%%')
plt.title('饼图')
plt.axis('equal')
plt.show()

2. 使用 seaborn 可视化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import seaborn as sns
# 热力图
corr = df.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.title('热力图')
plt.show()
# 成对关系图
sns.pairplot(df[['Age', 'Salary', 'Experience']])
plt.title('成对关系图')
plt.show()
# 分类柱状图
sns.barplot(x='City', y='Salary', data=df)
plt.title('分类柱状图')
plt.show()
# 小提琴图
sns.violinplot(x='City', y='Age', data=df)
plt.title('小提琴图')
plt.show()

八、完整代码示例

下面是一个完整的 pandas 基础使用示例,展示了从数据读取到分析的整个流程。

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 1. 创建示例数据
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Henry', 'Ivy', 'Jack'],
'Age': [25, 30, 35, 40, 45, 50, 55, 60, 65, 70],
'City': ['New York', 'London', 'Paris', 'Tokyo', 'Sydney', 'New York', 'London', 'Paris', 'Tokyo', 'Sydney'],
'Salary': [50000, 60000, 70000, 80000, 90000, 100000, 110000, 120000, 130000, 140000],
'Experience': [2, 5, 8, 11, 14, 17, 20, 23, 26, 29]
}
df = pd.DataFrame(data)
print("原始数据:")
print(df)
print()
# 2. 数据基本信息
print("数据基本信息:")
print(df.info())
print()
print("数据统计描述:")
print(df.describe())
print()
# 3. 数据清洗
# 假设添加一些缺失值
import copy
df_with_na = copy.deepcopy(df)
df_with_na.loc[2, 'Age'] = np.nan
df_with_na.loc[5, 'Salary'] = np.nan
print("包含缺失值的数据:")
print(df_with_na)
print()
print("缺失值情况:")
print(df_with_na.isnull().sum())
print()
# 处理缺失值
df_clean = df_with_na.fillna({
'Age': df_with_na['Age'].mean(),
'Salary': df_with_na['Salary'].median()
})
print("处理缺失值后的数据:")
print(df_clean)
print()
# 4. 数据筛选与查询
print("年龄大于50岁的数据:")
print(df_clean[df_clean['Age'] > 50])
print()
print("来自New York且薪资大于80000的数据:")
print(df_clean[(df_clean['City'] == 'New York') & (df_clean['Salary'] > 80000)])
print()
# 5. 数据分组与聚合
print("按城市分组的平均年龄和薪资:")
grouped = df_clean.groupby('City')
print(grouped[['Age', 'Salary']].mean())
print()
# 6. 数据可视化
# 薪资与经验的关系
df_clean.plot(kind='scatter', x='Experience', y='Salary', color='blue', marker='o', s=50)
plt.title('薪资与工作经验的关系')
plt.xlabel('工作经验(年)')
plt.ylabel('薪资(美元)')
plt.grid(True)
plt.show()
# 不同城市的薪资分布
sns.barplot(x='City', y='Salary', data=df_clean, palette='viridis')
plt.title('不同城市的平均薪资')
plt.xlabel('城市')
plt.ylabel('平均薪资(美元)')
plt.xticks(rotation=45)
plt.show()
# 年龄分布直方图
df_clean['Age'].plot(kind='hist', bins=5, color='green', alpha=0.7)
plt.title('年龄分布')
plt.xlabel('年龄')
plt.ylabel('人数')
plt.grid(True, alpha=0.3)
plt.show()
# 7. 导出结果
df_clean.to_csv('cleaned_data.csv', index=False)
print("数据已导出到cleaned_data.csv")

九、常见错误及解决方案

1. 缺失值处理错误

错误信息 :ValueError: Input contains NaN, infinity or a value too large for dtype('float64')

解决方案 :

  • 在进行数值计算前,先处理缺失值

  • 使用 df.dropna() 删除缺失值

  • 使用 df.fillna() 填充缺失值

  • 检查数据中是否有无穷大值,使用 df.replace([np.inf, -np.inf], np.nan) 处理

2. 数据类型不匹配

错误信息 :TypeError: unsupported operand type(s) for +: 'int' and 'str'

解决方案 :

  • 检查数据类型,使用 df.dtypes 查看

  • 转换数据类型,使用 df.astype() 或 pd.to_numeric()

  • 确保进行运算的列具有兼容的数据类型

3. 索引错误

错误信息 :KeyError: 'column_name'

解决方案 :

  • 检查列名是否正确,注意大小写

  • 使用 df.columns 查看所有列名

  • 确保列名没有空格或特殊字符

  • 使用 df.rename(columns={'old_name': 'new_name'}) 重命名列

错误信息 :IndexError: single positional indexer is out-of-bounds

解决方案 :

  • 检查索引范围,使用 df.shape 查看数据形状

  • 确保索引值在有效范围内

  • 使用.loc[] 或.iloc[] 进行安全索引

4. 内存不足

错误信息 :MemoryError

解决方案 :

  • 读取大数据时使用 chunksize 参数分块读取

  • 选择性读取需要的列,使用 usecols 参数

  • 转换数据类型为更节省内存的类型,如 int8、float16 等

  • 使用 df.memory_usage() 查看内存使用情况

5. 文件路径错误

错误信息 :FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'

解决方案 :

  • 检查文件路径是否正确

  • 确认文件存在

  • 使用绝对路径代替相对路径

  • 检查文件权限

十、进阶用法

1. 时间序列处理

1
2
3
4
5
6
7
8
9
10
11
12
13
# 创建时间序列索引
date_rng = pd.date_range(start='2026-01-01', end='2026-01-31', freq='D')
df = pd.DataFrame(date_rng, columns=['date'])
df['value'] = np.random.randint(0, 100, size=(len(date_rng)))
df.set_index('date', inplace=True)
# 时间序列重采样
df.resample('W').mean() # 按周重采样
df.resample('M').sum() # 按月重采样
# 移动窗口计算
df['rolling_mean'] = df['value'].rolling(window=7).mean() # 7天移动平均
df['rolling_std'] = df['value'].rolling(window=7).std() # 7天移动标准差
# 差分运算
df['diff'] = df['value'].diff() # 一阶差分

2. 多索引操作

1
2
3
4
5
6
# 创建多索引
df = pd.DataFrame({
'year': [2024, 2024, 2025, 2025],
'quarter': [1, 2, 1, 2],
'sales': [100, 200, 300, 400]
})

Python 操作 xlsx 表格完全指南

在日常工作和数据分析中,Excel 表格是最常用的数据存储和展示工具之一。Python 提供了多种库来操作 Excel 文件,特别是 xlsx 格式的文件。本文将详细介绍如何使用 Python 操作 xlsx 表格,包括常用库的使用方法、代码示例、常见错误及解决方案等。

一、依赖的库

Python 操作 xlsx 表格主要依赖以下几个库:

1. openpyxl

openpyxl 是一个用于读写 Excel 2010 xlsx/xlsm/xltx/xltm 文件的 Python 库,是目前最流行的 Excel 操作库之一。

安装方法 :

1
pip install openpyxl

版本要求 :

  • 推荐使用最新稳定版本

  • 目前稳定版本:v3.1.5(截至 2026 年 1 月)

2. pandas

pandas 是一个强大的数据分析库,也提供了方便的 Excel 文件读写功能,适合处理大量数据。

安装方法 :

1
pip install pandas

版本要求 :

  • 推荐版本:v2.2.4 或更高

3. xlsxwriter

xlsxwriter 是一个用于创建 Excel xlsx 文件的 Python 库,支持写入文本、数字、公式和超链接,以及创建图表等功能。

安装方法 :

1
pip install xlsxwriter

版本要求 :

  • 推荐版本:v3.2.0 或更高

4. xlrd(可选)

xlrd 用于读取旧版 Excel(.xls)文件,对于 xlsx 文件,pandas 1.2.0 + 版本已经不再依赖 xlrd,而是使用 openpyxl。

安装方法 :

1
pip install xlrd

版本要求 :

  • 推荐版本:v2.0.1 或更高

二、openpyxl 库使用

1. 工作簿操作

创建工作簿 :

1
2
3
4
5
from openpyxl import Workbook
# 创建一个新的工作簿
wb = Workbook()
# 保存工作簿
wb.save('example.xlsx')

打开现有工作簿 :

1
2
3
4
5
from openpyxl import load_workbook
# 打开现有工作簿
wb = load_workbook('example.xlsx')
# 获取工作簿中所有工作表名称
print(wb.sheetnames)

2. 工作表操作

创建工作表 :

1
2
3
4
5
6
# 创建一个新的工作表,默认在最后
ws1 = wb.create_sheet("Sheet1")
# 创建一个新的工作表,放在最前面
ws2 = wb.create_sheet("Sheet2", 0)
# 创建一个新的工作表,放在指定位置
ws3 = wb.create_sheet("Sheet3", 1)

选择工作表 :

1
2
3
4
# 通过名称选择工作表
ws = wb["Sheet1"]
# 获取活动工作表
ws = wb.active

重命名工作表 :

1
2
# 重命名工作表
ws.title = "新工作表名称"

删除工作表 :

1
2
3
4
# 删除工作表
wb.remove(ws)
# 或者使用del关键字
# del wb["Sheet1"]

3. 单元格操作

访问单元格 :

1
2
3
4
# 直接通过单元格地址访问
cell = ws["A1"]
# 通过行和列访问(行号从1开始)
cell = ws.cell(row=1, column=1)

设置单元格值 :

1
2
3
4
# 设置单元格值
ws["A1"] = "Hello, World!"
# 或者使用cell对象
ws.cell(row=1, column=2, value="Python")

获取单元格值 :

1
2
3
4
5
6
# 获取单元格值
value = ws["A1"].value
print(value)
# 或者使用cell对象
value = ws.cell(row=1, column=2).value
print(value)

访问多个单元格 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 访问一行
for cell in ws["A1:C1"]:
for c in cell:
print(c.value)
# 访问一列
for cell in ws["A1:A5"]:
for c in cell:
print(c.value)
# 访问一个区域
for row in ws["A1:C5"]:
for cell in row:
print(cell.value, end=" ")
print()
# 访问所有行
for row in ws.iter_rows(min_row=1, max_col=3, max_row=5):
for cell in row:
print(cell.value, end=" ")
print()
# 访问所有列
for col in ws.iter_cols(min_row=1, max_col=3, max_row=5):
for cell in col:
print(cell.value, end=" ")
print()

4. 数据读写

写入多行数据 :

1
2
3
4
5
6
7
8
9
10
11
# 准备数据
data = [
["姓名", "年龄", "城市"],
["Alice", 25, "New York"],
["Bob", 30, "London"],
["Charlie", 35, "Paris"],
["David", 40, "Tokyo"]
]
# 写入数据
for row in data:
ws.append(row)

读取所有数据 :

1
2
3
# 读取所有数据
for row in ws.iter_rows(values_only=True):
print(row)

5. 样式设置

设置字体样式 :

1
2
3
4
5
6
7
8
9
from openpyxl.styles import Font, Color
# 设置单元格字体
ws["A1"].font = Font(
name="Arial",
size=14,
bold=True,
italic=False,
color="FF0000" # 红色
)

设置对齐方式 :

1
2
3
4
5
6
7
from openpyxl.styles import Alignment
# 设置单元格对齐方式
ws["A1"].alignment = Alignment(
horizontal="center", # 水平居中
vertical="center", # 垂直居中
wrap_text=True # 自动换行
)

设置边框 :

1
2
3
4
5
6
7
8
9
10
from openpyxl.styles import Border, Side
# 创建边框样式
thin_border = Border(
left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin')
)
# 设置单元格边框
ws["A1"].border = thin_border

设置填充颜色 :

1
2
3
4
5
6
7
from openpyxl.styles import PatternFill
# 设置单元格填充颜色
ws["A1"].fill = PatternFill(
start_color="FFFF00", # 黄色
end_color="FFFF00",
fill_type="solid"
)

设置列宽和行高 :

1
2
3
4
5
# 设置列宽
ws.column_dimensions["A"].width = 20
ws.column_dimensions["B"].width = 15
# 设置行高
ws.row_dimensions[1].height = 30

三、pandas 库操作 Excel

1. 读取 Excel 文件

基本读取 :

1
2
3
4
import pandas as pd
# 读取Excel文件
df = pd.read_excel('example.xlsx')
print(df)

读取指定工作表 :

1
2
3
4
5
6
7
8
9
10
11
12
13
# 读取指定工作表
df = pd.read_excel('example.xlsx', sheet_name='Sheet1')
print(df)
# 读取多个工作表
dfs = pd.read_excel('example.xlsx', sheet_name=['Sheet1', 'Sheet2'])
print(dfs['Sheet1'])
print(dfs['Sheet2'])
# 读取所有工作表
dfs = pd.read_excel('example.xlsx', sheet_name=None)
for sheet_name, df in dfs.items():
print(f"工作表名: {sheet_name}")
print(df)
print()

读取指定区域 :

1
2
3
4
5
6
# 读取指定区域
df = pd.read_excel('example.xlsx', usecols='A:C', nrows=5)
print(df)
# 读取指定行和列
df = pd.read_excel('example.xlsx', skiprows=1, usecols=[0, 1, 2])
print(df)

2. 写入 Excel 文件

基本写入 :

1
2
3
4
5
6
7
8
9
# 准备数据
data = {
'姓名': ['Alice', 'Bob', 'Charlie', 'David'],
'年龄': [25, 30, 35, 40],
'城市': ['New York', 'London', 'Paris', 'Tokyo']
}
df = pd.DataFrame(data)
# 写入Excel文件
df.to_excel('output.xlsx', index=False)

写入多个工作表 :

1
2
3
4
5
6
# 创建ExcelWriter对象
with pd.ExcelWriter('output.xlsx') as writer:
# 写入第一个工作表
df1.to_excel(writer, sheet_name='Sheet1', index=False)
# 写入第二个工作表
df2.to_excel(writer, sheet_name='Sheet2', index=False)

写入指定位置 :

1
2
# 写入指定位置
df.to_excel('output.xlsx', sheet_name='Sheet1', startrow=1, startcol=2, index=False)

3. 高级操作

设置列宽 :

1
2
3
4
5
6
7
8
with pd.ExcelWriter('output.xlsx', engine='xlsxwriter') as writer:
df.to_excel(writer, sheet_name='Sheet1', index=False)
# 获取工作表对象
worksheet = writer.sheets['Sheet1']
# 设置列宽
worksheet.set_column('A:A', 20)
worksheet.set_column('B:B', 15)
worksheet.set_column('C:C', 25)

设置单元格格式 :

1
2
3
4
5
6
7
8
9
10
11
with pd.ExcelWriter('output.xlsx', engine='xlsxwriter') as writer:
df.to_excel(writer, sheet_name='Sheet1', index=False)
# 获取工作簿和工作表对象
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# 创建格式对象
bold_format = workbook.add_format({'bold': True, 'font_color': 'red'})
# 设置单元格格式
worksheet.write('A1', '姓名', bold_format)
worksheet.write('B1', '年龄', bold_format)
worksheet.write('C1', '城市', bold_format)

四、xlsxwriter 库使用

1. 创建工作簿和工作表

创建工作簿 :

1
2
3
4
5
6
7
import xlsxwriter
# 创建工作簿
workbook = xlsxwriter.Workbook('example.xlsx')
# 创建工作表
worksheet = workbook.add_worksheet()
# 关闭工作簿
workbook.close()

创建命名工作表 :

1
2
3
4
# 创建命名工作表
worksheet1 = workbook.add_worksheet() # 默认名称:Sheet1
worksheet2 = workbook.add_worksheet('Data') # 名称:Data
worksheet3 = workbook.add_worksheet('Report') # 名称:Report

2. 写入数据

写入文本和数字 :

1
2
3
4
5
6
7
8
# 写入文本
worksheet.write('A1', 'Hello')
worksheet.write('B1', 'World')
# 写入数字
worksheet.write(0, 2, 123)
worksheet.write(1, 0, 456)
worksheet.write(1, 1, 789)
worksheet.write(1, 2, 10.5)

写入公式 :

1
2
3
# 写入公式
worksheet.write('D2', '=SUM(A2:C2)')
worksheet.write('A4', '=AVERAGE(A2:A3)')

写入日期和时间 :

1
2
3
4
5
6
7
8
9
10
11
import datetime
# 写入日期
worksheet.write('A1', datetime.datetime(2026, 1, 24))
# 写入时间
worksheet.write('B1', datetime.datetime(2026, 1, 24, 10, 30, 0))
# 设置日期格式
date_format = workbook.add_format({'num_format': 'yyyy-mm-dd'})
worksheet.write('A2', datetime.datetime(2026, 1, 24), date_format)
# 设置时间格式
time_format = workbook.add_format({'num_format': 'hh:mm:ss'})
worksheet.write('B2', datetime.datetime(2026, 1, 24, 10, 30, 0), time_format)

写入超链接 :

1
2
3
# 写入超链接
worksheet.write_url('A1', 'https://www.example.com', string='Example Website')
worksheet.write_url('A2', 'mailto:example@example.com', string='Send Email')

3. 创建图表

创建柱状图 :

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
# 准备数据
worksheet.write('A1', '产品')
worksheet.write('B1', '销量')
worksheet.write('A2', '产品A')
worksheet.write('B2', 100)
worksheet.write('A3', '产品B')
worksheet.write('B3', 200)
worksheet.write('A4', '产品C')
worksheet.write('B4', 150)
# 创建柱状图
chart = workbook.add_chart({'type': 'column'})
# 添加数据系列
chart.add_series({
'name': '=Sheet1!$B$1',
'categories': '=Sheet1!$A$2:$A$4',
'values': '=Sheet1!$B$2:$B$4',
})
# 设置图表标题
chart.set_title({'name': '产品销量'})
# 设置x轴标题
chart.set_x_axis({'name': '产品'})
# 设置y轴标题
chart.set_y_axis({'name': '销量'})
# 插入图表到工作表
worksheet.insert_chart('D2', chart)

创建折线图 :

1
2
3
4
5
6
7
8
9
10
11
12
# 创建折线图
chart = workbook.add_chart({'type': 'line'})
# 添加数据系列
chart.add_series({
'name': '=Sheet1!$B$1',
'categories': '=Sheet1!$A$2:$A$4',
'values': '=Sheet1!$B$2:$B$4',
})
# 设置图表标题
chart.set_title({'name': '产品销量趋势'})
# 插入图表到工作表
worksheet.insert_chart('D2', chart)

创建饼图 :

1
2
3
4
5
6
7
8
9
10
11
12
13
# 创建饼图
chart = workbook.add_chart({'type': 'pie'})
# 添加数据系列
chart.add_series({
'name': '=Sheet1!$B$1',
'categories': '=Sheet1!$A$2:$A$4',
'values': '=Sheet1!$B$2:$B$4',
'data_labels': {'value': True},
})
# 设置图表标题
chart.set_title({'name': '产品销量占比'})
# 插入图表到工作表
worksheet.insert_chart('D2', chart)

五、完整代码示例

1. 示例 1:使用 openpyxl 读写 Excel

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, Border, Side
# 创建工作簿
wb = Workbook()
ws = wb.active
ws.title = "员工信息"
# 设置表头
headers = ["姓名", "年龄", "部门", "工资"]
for col, header in enumerate(headers, 1):
cell = ws.cell(row=1, column=col, value=header)
# 设置表头样式
cell.font = Font(bold=True, color="FFFFFF")
cell.fill = PatternFill(start_color="4F81BD", end_color="4F81BD", fill_type="solid")
cell.alignment = Alignment(horizontal="center", vertical="center")
cell.border = Border(
left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin')
)
# 准备数据
data = [
["张三", 28, "技术部", 8000],
["李四", 32, "销售部", 12000],
["王五", 25, "市场部", 7500],
["赵六", 35, "财务部", 9000],
["孙七", 29, "技术部", 8500]
]
# 写入数据
for row, row_data in enumerate(data, 2):
for col, value in enumerate(row_data, 1):
cell = ws.cell(row=row, column=col, value=value)
cell.alignment = Alignment(horizontal="center", vertical="center")
cell.border = Border(
left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin')
)
# 设置列宽
ws.column_dimensions["A"].width = 15
ws.column_dimensions["B"].width = 10
ws.column_dimensions["C"].width = 15
ws.column_dimensions["D"].width = 15
# 保存工作簿
wb.save("员工信息表.xlsx")
print("员工信息表已创建成功!")
# 读取工作簿
from openpyxl import load_workbook
wb = load_workbook("员工信息表.xlsx")
ws = wb.active
print("\n读取员工信息表:")
for row in ws.iter_rows(values_only=True):
print(row)

2. 示例 2:使用 pandas 处理 Excel 数据

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pandas as pd
# 准备数据
data = {
'姓名': ['张三', '李四', '王五', '赵六', '孙七'],
'年龄': [28, 32, 25, 35, 29],
'部门': ['技术部', '销售部', '市场部', '财务部', '技术部'],
'工资': [8000, 12000, 7500, 9000, 8500]
}
df = pd.DataFrame(data)
# 写入Excel文件
with pd.ExcelWriter('员工信息_pandas.xlsx', engine='xlsxwriter') as writer:
df.to_excel(writer, sheet_name='员工信息', index=False)
# 获取工作簿和工作表对象
workbook = writer.book
worksheet = writer.sheets['员工信息']
# 设置列宽
worksheet.set_column('A:A', 15)
worksheet.set_column('B:B', 10)
worksheet.set_column('C:C', 15)
worksheet.set_column('D:D', 15)
# 设置表头格式
header_format = workbook.add_format({
'bold': True,
'text_wrap': True,
'valign': 'top',
'fg_color': '#4F81BD',
'bg_color': '#4F81BD',
'font_color': '#FFFFFF',
'border': 1
})
# 设置表头
for col_num, value in enumerate(df.columns.values):
worksheet.write(0, col_num, value, header_format)
# 创建工资数据的柱状图
chart = workbook.add_chart({'type': 'column'})
chart.add_series({
'name': '=员工信息!$D$1',
'categories': '=员工信息!$A$2:$A$6',
'values': '=员工信息!$D$2:$D$6',
})
chart.set_title({'name': '员工工资'})
chart.set_x_axis({'name': '姓名'})
chart.set_y_axis({'name': '工资'})
# 插入图表
worksheet.insert_chart('F2', chart)
print("使用pandas创建的员工信息表已保存成功!")
# 读取Excel文件并进行数据分析
print("\n读取Excel文件并进行数据分析:")
df_read = pd.read_excel('员工信息_pandas.xlsx')
print("数据基本信息:")
print(df_read.info())
print("\n数据统计描述:")
print(df_read.describe())
print("\n各部门平均工资:")
print(df_read.groupby('部门')['工资'].mean())
print("\n工资最高的员工:")
print(df_read[df_read['工资'] == df_read['工资'].max()])

3. 示例 3:使用 xlsxwriter 创建带图表的 Excel

Python 操作 CSV 文件完全指南

CSV(Comma-Separated Values,逗号分隔值)是一种简单、通用的数据交换格式,广泛应用于数据分析、数据迁移和自动化脚本中。Python 提供了多种方式来操作 CSV 文件,从内置的 csv 模块到强大的第三方库如 pandas 和 numpy。本文将详细介绍如何使用 Python 操作 CSV 文件,包括读取、写入、修改和分析 CSV 数据。

一、依赖的库

Python 操作 CSV 文件主要依赖以下几个库:

1. csv(内置库)

csv 是 Python 内置的 CSV 文件处理库,无需额外安装,适合处理简单的 CSV 文件。

2. pandas

pandas 是一个强大的数据分析库,提供了高效的 CSV 文件读写和处理功能,适合处理大规模数据和复杂分析。

安装方法 :

1
pip install pandas

版本要求 :

  • 推荐版本:v2.2.4 或更高

3. numpy

numpy 是一个用于科学计算的库,也提供了 CSV 文件的读写功能,适合处理数值型数据。

安装方法 :

1
pip install numpy

版本要求 :

  • 推荐版本:v2.1.0 或更高

二、使用 Python 内置 csv 模块操作 CSV 文件

1. 写入 CSV 文件

基本写入 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import csv
# 数据
rows = [
['姓名', '年龄', '城市'],
['张三', 28, '北京'],
['李四', 32, '上海'],
['王五', 25, '广州'],
['赵六', 35, '深圳']
]
# 写入CSV文件
with open('people.csv', 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerows(rows)
print("CSV文件写入成功!")

使用 DictWriter 写入 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import csv
# 数据
data = [
{'姓名': '张三', '年龄': 28, '城市': '北京'},
{'姓名': '李四', '年龄': 32, '城市': '上海'},
{'姓名': '王五', '年龄': 25, '城市': '广州'}
]
# 字段名
fields = ['姓名', '年龄', '城市']
# 写入CSV文件
with open('people_dict.csv', 'w', newline='', encoding='utf-8') as file:
writer = csv.DictWriter(file, fieldnames=fields)
writer.writeheader() # 写入表头
writer.writerows(data) # 写入数据
print("使用DictWriter写入CSV文件成功!")

2. 读取 CSV 文件

基本读取 :

1
2
3
4
5
6
import csv
# 读取CSV文件
with open('people.csv', 'r', encoding='utf-8') as file:
reader = csv.reader(file)
for row in reader:
print(row)

使用 DictReader 读取 :

1
2
3
4
5
6
import csv
# 读取CSV文件
with open('people_dict.csv', 'r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
print(row['姓名'], row['年龄'], row['城市'])

3. 高级操作

指定分隔符 :

1
2
3
4
5
6
7
8
9
10
import csv
# 写入使用分号分隔的CSV文件
with open('people_semicolon.csv', 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file, delimiter=';')
writer.writerows(rows)
# 读取使用分号分隔的CSV文件
with open('people_semicolon.csv', 'r', encoding='utf-8') as file:
reader = csv.reader(file, delimiter=';')
for row in reader:
print(row)

处理引号 :

1
2
3
4
5
6
7
8
9
10
11
12
import csv
# 写入包含引号的数据
with open('quotes.csv', 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file, quoting=csv.QUOTE_ALL) # 所有字段加引号
writer.writerow(['姓名', '引言'])
writer.writerow(['张三', '"生活就像海洋"'])
writer.writerow(['李四', '"不经历风雨,怎么见彩虹"'])
# 读取包含引号的数据
with open('quotes.csv', 'r', encoding='utf-8') as file:
reader = csv.reader(file)
for row in reader:
print(row)

三、使用 pandas 库操作 CSV 文件

1. 读取 CSV 文件

基本读取 :

1
2
3
4
import pandas as pd
# 读取CSV文件
df = pd.read_csv('people.csv')
print(df)

读取指定列 :

1
2
3
# 读取指定列
df = pd.read_csv('people.csv', usecols=['姓名', '城市'])
print(df)

读取指定行数 :

1
2
3
# 读取前3行
df = pd.read_csv('people.csv', nrows=3)
print(df)

处理缺失值 :

1
2
3
4
5
6
# 读取CSV文件,将'NA'和'缺失'视为缺失值
df = pd.read_csv('people.csv', na_values=['NA', '缺失'])
print(df)
# 填充缺失值
df.fillna(0, inplace=True)
print(df)

设置索引列 :

1
2
3
# 使用'姓名'列作为索引
df = pd.read_csv('people.csv', index_col='姓名')
print(df)

2. 写入 CSV 文件

基本写入 :

1
2
3
4
5
6
7
8
9
10
11
import pandas as pd
# 创建DataFrame
data = {
'姓名': ['张三', '李四', '王五', '赵六'],
'年龄': [28, 32, 25, 35],
'城市': ['北京', '上海', '广州', '深圳']
}
df = pd.DataFrame(data)
# 写入CSV文件
df.to_csv('people_pandas.csv', index=False, encoding='utf-8')
print("使用pandas写入CSV文件成功!")

写入指定格式 :

1
2
3
4
# 写入CSV文件,设置保留两位小数
df.to_csv('people_format.csv', index=False, float_format='%.2f', encoding='utf-8')
# 写入CSV文件,使用分号分隔
df.to_csv('people_semicolon_pandas.csv', index=False, sep=';', encoding='utf-8')

3. 数据处理

筛选数据 :

1
2
3
4
5
6
# 筛选年龄大于30的数据
df_filtered = df[df['年龄'] > 30]
print(df_filtered)
# 筛选来自北京或上海的数据
df_filtered = df[df['城市'].isin(['北京', '上海'])]
print(df_filtered)

排序数据 :

1
2
3
4
5
6
# 按年龄升序排序
df_sorted = df.sort_values(by='年龄')
print(df_sorted)
# 按年龄降序排序
df_sorted = df.sort_values(by='年龄', ascending=False)
print(df_sorted)

分组统计 :

1
2
3
# 按城市分组,计算平均年龄
df_grouped = df.groupby('城市')['年龄'].mean()
print(df_grouped)

添加新列 :

1
2
3
# 添加新列
df['年龄分类'] = df['年龄'].apply(lambda x: '青年' if x < 30 else '中年')
print(df)

四、使用 numpy 库操作 CSV 文件

1. 读取 CSV 文件

基本读取 :

1
2
3
4
import numpy as np
# 读取CSV文件
data = np.genfromtxt('people.csv', delimiter=',', dtype=str)
print(data)

跳过表头 :

1
2
3
# 读取CSV文件,跳过表头
data = np.genfromtxt('people.csv', delimiter=',', dtype=str, skip_header=1)
print(data)

指定数据类型 :

1
2
3
4
5
6
7
# 读取CSV文件,指定数据类型
# 第一列是字符串,第二列是整数,第三列是字符串
data = np.genfromtxt('people.csv', delimiter=',', dtype=[('name', 'U10'), ('age', int), ('city', 'U10')], skip_header=1)
print(data)
print(data['name'])
print(data['age'])
print(data['city'])

2. 写入 CSV 文件

基本写入 :

1
2
3
4
5
6
7
8
9
10
11
import numpy as np
# 创建数据
data = np.array([
['姓名', '年龄', '城市'],
['张三', '28', '北京'],
['李四', '32', '上海'],
['王五', '25', '广州']
])
# 写入CSV文件
np.savetxt('people_numpy.csv', data, delimiter=',', fmt='%s', encoding='utf-8')
print("使用numpy写入CSV文件成功!")

写入数值数据 :

1
2
3
4
5
6
7
8
# 创建数值数据
numeric_data = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
# 写入CSV文件,保留两位小数
np.savetxt('numeric_data.csv', numeric_data, delimiter=',', fmt='%.2f', encoding='utf-8')

五、完整代码示例

1. 示例 1:使用 csv 模块管理学生成绩

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
import csv
# 学生成绩数据
students = [
{'姓名': '张三', '语文': 85, '数学': 92, '英语': 78},
{'姓名': '李四', '语文': 90, '数学': 88, '英语': 95},
{'姓名': '王五', '语文': 76, '数学': 85, '英语': 82},
{'姓名': '赵六', '语文': 92, '数学': 94, '英语': 89}
]
fields = ['姓名', '语文', '数学', '英语']
# 写入学生成绩到CSV文件
with open('students_scores.csv', 'w', newline='', encoding='utf-8') as file:
writer = csv.DictWriter(file, fieldnames=fields)
writer.writeheader()
writer.writerows(students)
print("学生成绩已写入CSV文件!")
# 读取并分析学生成绩
print("\n读取并分析学生成绩:")
total_scores = {}
subjects = ['语文', '数学', '英语']
with open('students_scores.csv', 'r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for student in reader:
name = student['姓名']
total = sum(int(student[subj]) for subj in subjects)
avg = total / len(subjects)
total_scores[name] = {'总分': total, '平均分': avg}
print(f"{name}: 总分={total}, 平均分={avg:.2f}")
# 找出总分最高的学生
top_student = max(total_scores.items(), key=lambda x: x[1]['总分'])
print(f"\n总分最高的学生:{top_student[0]},总分={top_student[1]['总分']}")

2. 示例 2:使用 pandas 分析销售数据

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
32
33
34
35
import pandas as pd
# 创建销售数据
sales_data = {
'产品名称': ['A产品', 'B产品', 'C产品', 'A产品', 'B产品', 'C产品'],
'销售日期': ['2026-01-01', '2026-01-01', '2026-01-01', '2026-01-02', '2026-01-02', '2026-01-02'],
'销售数量': [100, 150, 200, 120, 180, 220],
'销售金额': [5000, 7500, 10000, 6000, 9000, 11000]
}
df = pd.DataFrame(sales_data)
# 写入CSV文件
df.to_csv('sales_data.csv', index=False, encoding='utf-8')
print("销售数据已写入CSV文件!")
# 读取销售数据
df_sales = pd.read_csv('sales_data.csv', encoding='utf-8')
print("\n销售数据:")
print(df_sales)
# 数据分析
print("\n数据分析:")
# 1. 按产品分组,计算总销售数量和总销售金额
product_sales = df_sales.groupby('产品名称')[['销售数量', '销售金额']].sum()
print("按产品分组的销售情况:")
print(product_sales)
# 2. 按日期分组,计算总销售金额
date_sales = df_sales.groupby('销售日期')['销售金额'].sum()
print("\n按日期分组的销售金额:")
print(date_sales)
# 3. 计算每个产品的平均销售单价
df_sales['单价'] = df_sales['销售金额'] / df_sales['销售数量']
avg_price = df_sales.groupby('产品名称')['单价'].mean()
print("\n每个产品的平均销售单价:")
print(avg_price)
# 4. 找出销售数量最多的产品
top_product = df_sales.groupby('产品名称')['销售数量'].sum().idxmax()
top_sales = df_sales.groupby('产品名称')['销售数量'].sum().max()
print(f"\n销售数量最多的产品:{top_product},销售数量={top_sales}")

六、常见错误及解决方案

1. 编码错误

错误信息 :UnicodeDecodeError: 'gbk' codec can't decode byte 0xa0 in position 20: illegal multibyte sequence

解决方案 :

  • 在打开文件时指定正确的编码,如 encoding='utf-8' 或 encoding='gbk'

  • 使用 errors='ignore' 忽略无法解码的字符(不推荐)

  • 使用 chardet 库检测文件编码:

1
2
3
4
import chardet
with open('file.csv', 'rb') as f:
result = chardet.detect(f.read())
print(result['encoding']) # 输出文件编码

2. 分隔符错误

错误信息 :ValueError: Expected 3 fields in line 2, saw 5

解决方案 :

  • 检查 CSV 文件的分隔符是否正确

  • 在读取文件时指定正确的分隔符,如 delimiter=';' 或 delimiter='\t'

  • 使用 csv.Sniffer 自动检测分隔符:

1
2
3
4
5
import csv
with open('file.csv', 'r', encoding='utf-8') as f:
dialect = csv.Sniffer().sniff(f.read(1024))
f.seek(0) # 重置文件指针
reader = csv.reader(f, dialect)

3. 字段名不匹配

错误信息 :KeyError: '字段名'

解决方案 :

  • 检查 CSV 文件的表头是否正确

  • 使用 reader.fieldnames 查看实际的字段名

  • 确保代码中使用的字段名与 CSV 文件中的表头一致

4. 类型转换错误

错误信息 :ValueError: invalid literal for int() with base 10: '字符串'

解决方案 :

  • 在转换数据类型前检查数据格式

  • 使用 try-except 处理异常值:

1
2
3
4
try:
age = int(row['年龄'])
except ValueError:
age = 0 # 或其他默认值
  • 使用 pandas 的 pd.to_numeric() 函数安全转换:
1
df['年龄'] = pd.to_numeric(df['年龄'], errors='coerce')

5. 文件路径错误

错误信息 :FileNotFoundError: [Errno 2] No such file or directory: 'file.csv'

解决方案 :

  • 检查文件路径是否正确

  • 使用绝对路径代替相对路径

  • 确保文件存在于指定位置

七、最佳实践

  1. 选择合适的库 :

    • 对于简单的 CSV 操作,使用内置的 csv 模块

    • 对于数据分析和处理,使用 pandas

    • 对于数值型数据,使用 numpy

  2. 使用上下文管理器 :

    • 始终使用 with 语句打开文件,确保文件正确关闭

    • 避免手动打开和关闭文件,减少资源泄漏风险

  3. 指定编码 :

    • 打开文件时明确指定编码,如 encoding='utf-8'

    • 确保写入和读取使用相同的编码

  4. 处理缺失值 :

    • 在读取 CSV 文件时,指定缺失值标记

    • 对缺失值进行适当处理,如填充默认值或删除

  5. 优化性能 :

    • 对于大型 CSV 文件,使用 pandas 的 chunksize 参数分块读取

    • 避免在循环中频繁读写文件

    • 使用向量化操作代替循环

  6. 数据验证 :

    • 在写入 CSV 文件前验证数据格式

    • 在读取 CSV 文件后检查数据完整性

    • 使用类型注解提高代码可读性和可维护性

  7. 文档和注释 :

    • 为 CSV 文件添加说明文档

    • 在代码中添加注释,说明数据格式和处理逻辑

    • 保存数据字典,说明每个字段的含义和格式

八、总结

本文详细介绍了 Python 操作 CSV 文件的三种主要方式:

  1. 内置 csv 模块 :适合简单的 CSV 读写操作,无需额外安装库,使用方便

  2. pandas 库 :适合大规模数据处理和分析,提供了丰富的数据操作功能

  3. numpy 库 :适合数值型数据的读写,性能高效

通过学习本文,你应该能够:

  • 使用不同的库读写 CSV 文件

  • 处理不同格式的 CSV 数据

  • 进行基本的数据清洗和分析

  • 解决常见的 CSV 操作错误

  • 遵循最佳实践编写高效、可靠的 CSV 处理代码

CSV 文件作为一种简单、通用的数据格式,在数据处理和分析中扮演着重要角色。掌握 Python 操作 CSV 文件的技能,将有助于你更高效地处理各种数据任务,提高工作效率。

九、参考资料

  1. Python 官方文档 - csv 模块

  2. pandas 官方文档 - CSV 读写

  3. numpy 官方文档 - CSV 读写

  4. Real Python - Working with CSV Files in Python

  5. W3Schools - Python CSV

  6. CSV 文件格式规范

  7. Python 数据分析实战

通过 Python 调用 OpenAI API 的实战

在人工智能时代,OpenAI 提供的 API 服务为开发者提供了强大的 AI 能力。无论是聊天机器人、图像生成、语音处理还是代码生成,OpenAI API 都能提供高效、准确的解决方案。本文将详细介绍如何使用 Python 调用 OpenAI API,包括依赖库、调用方法、代码示例以及可能出现的错误和解决方案。

一、依赖的库

要使用 Python 调用 OpenAI API,我们需要安装 OpenAI 官方提供的 Python 库。

1. openai

openai 是 OpenAI 官方提供的 Python 客户端库,用于简化 API 调用流程。它封装了各种 API 端点,提供了便捷的方法来调用 OpenAI 的各种服务。

安装方法 :

1
pip install openai

版本要求 :

  • 推荐使用最新版本,以获得最佳性能和最新功能

  • 目前稳定版本:v1.35.10(截至 2026 年 1 月)

二、调用方法

1. 基本配置

在使用 OpenAI API 之前,需要进行基本配置,主要是设置 API 密钥。

步骤 1:获取 API 密钥

步骤 2:设置 API 密钥

方法 1:直接在代码中设置

1
2
3
4
5
from openai import OpenAI
# 初始化客户端
client = OpenAI(
api_key="your-api-key-here" # 替换为你的API密钥
)

方法 2:使用环境变量(推荐)

1
2
3
4
5
6
import os
from openai import OpenAI
# 从环境变量获取API密钥
client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY")
)

设置环境变量的方法:

  • Windows:setx OPENAI_API_KEY "your-api-key-here"

  • Linux/macOS:export OPENAI_API_KEY="your-api-key-here"

2. 聊天完成(Chat Completions)

聊天完成是 OpenAI API 中最常用的功能,用于生成对话式响应。

基本调用示例 :

1
2
3
4
5
6
7
8
9
10
# 调用聊天完成API
response = client.chat.completions.create(
model="gpt-4o", # 使用的模型
messages=[
{"role": "system", "content": "你是一个 helpful 的助手。"},
{"role": "user", "content": "介绍一下Python的主要特点。"}
]
)
# 输出响应结果
print(response.choices[0].message.content)

参数说明 :

  • model:使用的模型名称,如 gpt-4o、gpt-3.5-turbo 等

  • messages:对话历史,包含角色(system/user/assistant)和内容

  • temperature:控制生成文本的随机性,范围 0-2,值越高越随机

  • max_tokens:生成文本的最大令牌数

3. 图片生成(Image Generation)

OpenAI API 提供了图片生成功能,可以根据文本描述生成图片。

基本调用示例 :

1
2
3
4
5
6
7
8
9
10
11
# 调用图片生成API
response = client.images.generate(
model="dall-e-3", # 使用的模型
prompt="一只可爱的柯基犬在草地上奔跑,阳光明媚,水彩画风格", # 图片描述
size="1024x1024", # 图片尺寸
quality="standard", # 图片质量
n=1, # 生成图片数量
)
# 获取图片URL
image_url = response.data[0].url
print(f"生成的图片URL:{image_url}")

参数说明 :

  • model:使用的模型,如 dall-e-3、dall-e-2

  • prompt:图片描述,越详细生成效果越好

  • size:图片尺寸,如 1024x1024、1792x1024 等

  • quality:图片质量,standard 或 hd

  • n:生成图片数量,范围 1-10

4. 语音转文本(Speech to Text)

OpenAI API 提供了语音转文本功能,可以将音频文件转换为文本。

基本调用示例 :

1
2
3
4
5
6
7
8
9
# 调用语音转文本API
with open("audio.mp3", "rb") as audio_file:
response = client.audio.transcriptions.create(
model="whisper-1", # 使用的模型
file=audio_file, # 音频文件
response_format="text" # 输出格式
)
# 输出转换结果
print(f"转换结果:{response}")

参数说明 :

  • model:使用的模型,目前主要是 whisper-1

  • file:音频文件对象

  • response_format:输出格式,如 text、json、srt 等

  • language:音频语言,可选参数,如 zh、en 等

三、完整代码示例

1. 聊天完成完整示例

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
import os
from openai import OpenAI
# 初始化客户端
client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY")
)
def chat_completion_example():
"""聊天完成示例"""
try:
# 调用API
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "你是一个Python编程助手。"},
{"role": "user", "content": "写一个Python函数,用于计算斐波那契数列的第n项。"}
],
temperature=0.7,
max_tokens=500
)
# 输出结果
print("\n=== 聊天完成示例 ===")
print("问题:写一个Python函数,用于计算斐波那契数列的第n项。")
print(f"回答:{response.choices[0].message.content}")
except Exception as e:
print(f"错误:{e}")
if __name__ == "__main__":
chat_completion_example()

2. 图片生成完整示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import os
from openai import OpenAI
# 初始化客户端
client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY")
)
def image_generation_example():
"""图片生成示例"""
try:
# 调用API
response = client.images.generate(
model="dall-e-3",
prompt="一只可爱的柯基犬在草地上奔跑,阳光明媚,水彩画风格",
size="1024x1024",
quality="standard",
n=1,
)
# 输出结果
print("\n=== 图片生成示例 ===")
print(f"生成的图片URL:{response.data[0].url}")
except Exception as e:
print(f"错误:{e}")
if __name__ == "__main__":
image_generation_example()

四、常见错误及解决方案

1. API 密钥错误

错误信息 :openai.AuthenticationError: Invalid authentication credentials

解决方案 :

  • 检查 API 密钥是否正确,确保没有多余的空格或换行符

  • 确保 API 密钥没有过期或被撤销

  • 检查环境变量是否正确设置

2. 请求频率限制

错误信息 :openai.RateLimitError: Rate limit exceeded

解决方案 :

  • 减少请求频率,添加适当的延迟

  • 考虑升级 API 套餐,提高速率限制

  • 使用指数退避策略处理重试

3. 模型不存在

错误信息 :openai.BadRequestError: The model 'gpt-5' does not exist

解决方案 :

  • 检查模型名称是否正确

  • 确认你有权限访问该模型

  • 查看 OpenAI 官网文档,了解可用的模型列表

4. 输入格式错误

错误信息 :openai.BadRequestError: Invalid input format

解决方案 :

  • 检查请求参数是否符合 API 要求

  • 确保 messages 格式正确,包含 role 和 content 字段

  • 检查文件格式是否支持(如语音转文本)

5. 上下文窗口超出

错误信息 :openai.BadRequestError: This model's maximum context length is 128000 tokens

解决方案 :

  • 减少输入文本长度

  • 使用更适合长文本的模型

  • 实现对话历史截断机制

五、进阶用法

1. 异步调用

对于高并发场景,可以使用异步调用提高效率。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import os
import asyncio
from openai import AsyncOpenAI
# 初始化异步客户端
async_client = AsyncOpenAI(
api_key=os.getenv("OPENAI_API_KEY")
)
async def async_chat_completion():
"""异步聊天完成"""
response = await async_client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "介绍一下异步编程的优势。"}
]
)
return response.choices[0].message.content
# 执行异步函数
async def main():
result = await async_chat_completion()
print(result)
asyncio.run(main())

2. 流式响应

对于长时间运行的请求,可以使用流式响应实时获取结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY")
)
def streaming_chat_completion():
"""流式聊天完成"""
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "写一个Python函数,用于生成100以内的素数。"}
],
stream=True # 启用流式响应
)
print("\n=== 流式响应示例 ===")
print("回答:", end="")
# 逐块获取响应
for chunk in response:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
if __name__ == "__main__":
streaming_chat_completion()

3. 自定义代理

如果在受限网络环境下,可以设置自定义代理。

1
2
3
4
5
from openai import OpenAI
client = OpenAI(
api_key="your-api-key-here",
base_url="https://your-proxy-server.com/v1" # 自定义代理URL
)

六、总结

通过 Python 调用 OpenAI API 是一项强大的技能,可以为各种应用提供 AI 能力。本文介绍了 OpenAI 库的安装和配置,详细说明了聊天完成、图片生成、语音转文本等常见功能的调用方法,并提供了完整的代码示例。同时,我们还讨论了常见错误及解决方案,以及异步调用、流式响应等进阶用法。

在实际应用中,需要根据具体需求选择合适的模型和参数,并注意 API 密钥的安全管理。随着 OpenAI API 的不断更新,建议定期查看官方文档,了解最新功能和最佳实践。

希望本文能够帮助你掌握 Python 调用 OpenAI API 的技能,为你的项目开发提供便利!

七、参考资料

  1. OpenAI 官方文档

  2. OpenAI Python 库文档

  3. OpenAI API 密钥管理

  4. OpenAI 模型列表

  5. OpenAI API 速率限制

FastAPI 基础应用

1. FastAPI 简介

FastAPI 是一个现代化、高性能的 Web 框架,用于构建 API,基于 Python 3.6 + 的类型提示和异步支持。它具有以下特点:

  • 快速 :基于 Starlette 和 Pydantic,性能可与 NodeJS 和 Go 媲美

  • 自动 API 文档 :自动生成交互式 API 文档(Swagger UI 和 ReDoc)

  • 类型安全 :利用 Python 类型提示进行数据验证和自动生成文档

  • 异步支持 :原生支持异步操作,提高并发处理能力

  • 简单易用 :API 设计简洁,学习曲线平缓

  • 社区活跃 :持续更新和完善,拥有丰富的生态系统

2. 环境搭建与依赖安装

2.1 安装 Python

FastAPI 需要 Python 3.6+,建议使用 Python 3.8 或更高版本。可以从 Python 官网下载并安装。

2.2 安装 FastAPI 和依赖

使用 pip 安装 FastAPI 及其依赖:

1
pip install fastapi uvicorn
  • fastapi:FastAPI 框架本身

  • uvicorn:ASGI 服务器,用于运行 FastAPI 应用

3. 第一个 FastAPI 应用

3.1 创建基础应用

创建一个名为 main.py 的文件,编写以下代码:

1
2
3
4
5
6
7
8
9
10
11
from fastapi import FastAPI
# 创建FastAPI实例
app = FastAPI()
# 定义根路径路由
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI!"}
# 定义带路径参数的路由
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}

3.2 运行应用

在终端中执行以下命令运行应用:

1
uvicorn main:app --reload
  • main:Python 文件名(不含.py 扩展名)

  • app:FastAPI 实例名称

  • --reload:启用自动重载,代码修改后自动重启服务器

运行成功后,在浏览器中访问:

4. 路由与 HTTP 方法

FastAPI 支持所有常见的 HTTP 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from fastapi import FastAPI
app = FastAPI()
# GET请求
@app.get("/items")
def get_items():
return {"method": "GET", "action": "获取所有物品"}
# POST请求
@app.post("/items")
def create_item():
return {"method": "POST", "action": "创建物品"}
# PUT请求
@app.put("/items/{item_id}")
def update_item(item_id: int):
return {"method": "PUT", "action": "更新物品", "item_id": item_id}
# DELETE请求
@app.delete("/items/{item_id}")
def delete_item(item_id: int):
return {"method": "DELETE", "action": "删除物品", "item_id": item_id}

5. 请求处理

5.1 路径参数

路径参数是 URL 路径的一部分,用于标识特定资源:

1
2
3
4
5
6
7
@app.get("/users/{user_id}/items/{item_id}")
def get_user_item(user_id: int, item_id: int, q: str = None):
return {
"user_id": user_id,
"item_id": item_id,
"q": q
}

5.2 查询参数

查询参数是 URL 中 ? 后面的键值对,用于过滤、排序等操作:

1
2
3
4
@app.get("/items/")
def get_items(skip: int = 0, limit: int = 10):
# skip和limit是查询参数,有默认值
return {"skip": skip, "limit": limit}

5.3 请求体

使用 Pydantic 模型定义请求体:

1
2
3
4
5
6
7
8
9
10
from pydantic import BaseModel
# 定义Pydantic模型
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
@app.post("/items/")
def create_item(item: Item):
return item

5.4 表单数据

安装依赖:

1
pip install python-multipart

使用 Form 类处理表单数据:

1
2
3
4
from fastapi import Form
@app.post("/login/")
def login(username: str = Form(...), password: str = Form(...)):
return {"username": username, "password": "******"}

5.5 文件上传

使用 UploadFile 类处理文件上传:

1
2
3
4
5
6
7
from fastapi import UploadFile, File
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
return {
"filename": file.filename,
"content_type": file.content_type
}

6. 响应处理

6.1 返回 JSON 响应

FastAPI 默认将 Python 字典转换为 JSON 响应:

1
2
3
@app.get("/items/{item_id}")
def read_item(item_id: int):
return {"item_id": item_id, "name": "Item Name"}

6.2 自定义响应状态码

使用 status 参数指定响应状态码:

1
2
3
4
from fastapi import status
@app.post("/items/", status_code=status.HTTP_201_CREATED)
def create_item(item: Item):
return item

6.3 自定义响应头

使用 Response 类自定义响应头:

1
2
3
4
5
from fastapi import Response
@app.get("/items/{item_id}")
def read_item(item_id: int, response: Response):
response.headers["X-Item-ID"] = str(item_id)
return {"item_id": item_id}

6.4 直接返回响应

使用 JSONResponse、HTMLResponse 等直接返回响应:

1
2
3
4
5
6
7
from fastapi.responses import JSONResponse, HTMLResponse
@app.get("/json/")
def get_json():
return JSONResponse(content={"message": "Hello"}, status_code=200)
@app.get("/html/")
def get_html():
return HTMLResponse(content="<h1>Hello, FastAPI!</h1>")

7. Pydantic 模型

Pydantic 是 FastAPI 的核心依赖之一,用于数据验证和设置管理。

7.1 基础模型定义

1
2
3
4
5
6
7
8
from pydantic import BaseModel, Field
class User(BaseModel):
username: str
email: str
full_name: str = None
disabled: bool = False
# 使用Field添加额外验证和描述
age: int = Field(..., ge=0, le=120, description="用户年龄")

7.2 嵌套模型

1
2
3
4
5
class Address(BaseModel):
city: str
country: str
class UserWithAddress(User):
address: Address

7.3 模型继承

1
2
3
4
5
6
7
8
9
10
class BaseItem(BaseModel):
name: str
description: str = None
class ItemIn(BaseItem):
price: float
tax: float = None
class ItemOut(BaseItem):
id: int
price: float
tax: float = None

8. 中间件

中间件是在请求和响应之间执行的代码,可以用于日志记录、身份验证等。

8.1 创建自定义中间件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
# 添加CORS中间件
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 允许所有来源,生产环境应指定具体域名
allow_credentials=True,
allow_methods=["*"], # 允许所有HTTP方法
allow_headers=["*"], # 允许所有请求头
)
# 自定义中间件
@app.middleware("http")
async def add_process_time_header(request, call_next):
import time
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(process_time)
return response

9. 异常处理

9.1 抛出 HTTP 异常

1
2
3
4
5
6
7
8
9
10
from fastapi import HTTPException
@app.get("/items/{item_id}")
def read_item(item_id: int):
if item_id not in items:
raise HTTPException(
status_code=404,
detail="Item not found",
headers={"X-Error": "Item not found"}
)
return {"item_id": item_id}

9.2 自定义异常处理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from fastapi import Request
from fastapi.responses import JSONResponse
# 自定义异常类
class CustomException(Exception):
def __init__(self, name: str):
self.name = name
# 注册异常处理器
@app.exception_handler(CustomException)
async def custom_exception_handler(request: Request, exc: CustomException):
return JSONResponse(
status_code=418,
content={"message": f"I am a teapot, {exc.name}"}
)
@app.get("/teapot")
def get_teapot():
raise CustomException(name="FastAPI")

10. API 文档

FastAPI 自动生成两种 API 文档:

10.1 Swagger UI

访问 http://127.0.0.1:8000/docs,可以看到交互式 API 文档,支持直接测试 API。

10.2 ReDoc

访问 http://127.0.0.1:8000/redoc,可以看到更简洁的 API 文档。

10.3 自定义文档

可以自定义文档的标题和描述:

1
2
3
4
5
app = FastAPI(
title="My FastAPI Application",
description="This is a simple FastAPI application",
version="0.1.0",
)

11. 部署 FastAPI 应用

11.1 使用 Uvicorn 部署

生产环境部署命令:

1
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4
  • --workers:指定工作进程数,建议为 CPU 核心数

11.2 使用 Gunicorn + Uvicorn

安装依赖:

1
pip install gunicorn

运行命令:

1
gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app

11.3 容器化部署

创建 Dockerfile:

1
2
3
4
5
6
FROM python:3.9
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

创建 requirements.txt:

1
2
fastapi
uvicorn

构建和运行 Docker 容器:

1
2
docker build -t fastapi-app .
docker run -d -p 8000:8000 fastapi-app

12. 常见问题与解决方案

12.1 跨域问题

使用 CORS 中间件解决:

1
2
3
4
5
6
7
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

12.2 类型错误

确保所有函数参数都有正确的类型提示,Pydantic 模型字段也有正确的类型。

12.3 依赖注入错误

确保依赖项的返回类型与预期一致,使用 Depends 类正确注入依赖。

13. 进阶主题

13.1 依赖注入

使用 Depends 类实现依赖注入:

1
2
3
4
5
6
7
8
9
10
from fastapi import Depends
def get_db():
db = "Database Connection"
try:
yield db
finally:
db = "Closed"
@app.get("/items/")
def get_items(db: str = Depends(get_db)):
return {"db": db}

13.2 安全认证

使用 OAuth2PasswordBearer 实现 OAuth2 认证:

1
2
3
4
5
6
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
@app.get("/users/me")
def read_users_me(token: str = Depends(oauth2_scheme)):
return {"token": token}

13.3 测试 FastAPI 应用

使用 TestClient 测试 FastAPI 应用:

1
2
3
4
5
6
7
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_read_main():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello, FastAPI!"}

14. 总结

FastAPI 是一个现代化、高性能的 Web 框架,具有以下优势:

  • 快速的开发速度

  • 自动生成 API 文档

  • 类型安全

  • 异步支持

  • 简单易用

  • 丰富的生态系统

通过本文的介绍,你已经了解了 FastAPI 的基础应用,包括:

  • 环境搭建与依赖安装

  • 第一个 FastAPI 应用

  • 路由与 HTTP 方法

  • 请求处理(路径参数、查询参数、请求体、表单数据、文件上传)

  • 响应处理

  • Pydantic 模型

  • 中间件

  • 异常处理

  • API 文档

  • 部署方式

  • 常见问题与解决方案

FastAPI 是构建现代 API 的理想选择,无论是小型项目还是大型应用,都能提供高效、可靠的支持。

15. 参考资料

通过 Python 调用邮件服务的实战

在现代软件开发中,自动发送邮件是一项常见的需求。无论是系统监控告警、用户注册验证、还是定时报告发送,Python 都提供了强大的库来实现邮件服务的调用。本文将详细介绍如何使用 Python 调用邮件服务,包括依赖库、调用方法、代码示例以及可能出现的错误和解决方案。

一、依赖的库

Python 标准库中已经包含了用于发送邮件的模块,主要有以下几个:

1. smtplib

smtplib 是 Python 标准库中用于发送邮件的核心模块,它实现了 SMTP(Simple Mail Transfer Protocol)客户端协议,可以连接到 SMTP 服务器并发送邮件。

2. email

email 模块用于构建和解析邮件消息,包括邮件头、邮件正文、附件等。它提供了一系列类来创建结构化的邮件内容。

3. ssl

ssl 模块用于创建安全的 SSL/TLS 连接,在发送加密邮件时使用。

二、调用方法

1. 基本邮件发送

以下是使用 Python 发送基本文本邮件的步骤:

步骤 1:导入所需模块

1
2
3
import smtplib
from email.mime.text import MIMEText
from email.header import Header

步骤 2:配置邮件服务器信息

1
2
3
4
5
# 邮件服务器配置
smtp_server = 'smtp.qq.com' # QQ邮箱SMTP服务器
smtp_port = 465 # SSL端口
username = 'your_email@qq.com' # 发件人邮箱
password = 'your_password' # 邮箱密码或授权码

步骤 3:创建邮件内容

1
2
3
4
5
6
7
8
# 邮件内容
subject = 'Python邮件测试'
body = '这是一封通过Python发送的测试邮件!'
# 创建MIMEText对象
msg = MIMEText(body, 'plain', 'utf-8')
msg['From'] = Header('发件人名称', 'utf-8')
msg['To'] = Header('收件人名称', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')

步骤 4:发送邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
try:
# 创建SSL连接
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
# 登录邮件服务器
server.login(username, password)
# 发送邮件
server.sendmail(username, ['recipient@example.com'], msg.as_string())
print('邮件发送成功!')
except Exception as e:
print(f'邮件发送失败:{e}')
finally:
# 关闭连接
server.quit()

2. 发送 HTML 格式邮件

如果需要发送 HTML 格式的邮件,可以将 MIMEText 的 subtype 参数设置为’html’:

1
2
3
4
5
6
7
8
9
10
11
12
# HTML邮件内容
html_body = """
<html>
<body>
<h1>Python HTML邮件测试</h1>
<p>这是一封<strong>HTML格式</strong>的测试邮件!</p>
<a href="https://www.python.org">访问Python官网</a>
</body>
</html>
"""
# 创建HTML邮件
msg = MIMEText(html_body, 'html', 'utf-8')

3. 发送带附件的邮件

发送带附件的邮件需要使用 email.mime.multipart.MIMEMultipart 类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
# 创建MIMEMultipart对象
msg = MIMEMultipart()
msg['From'] = Header('发件人名称', 'utf-8')
msg['To'] = Header('收件人名称', 'utf-8')
msg['Subject'] = Header('带附件的Python邮件测试', 'utf-8')
# 添加正文
body = '这是一封带附件的测试邮件!'
msg.attach(MIMEText(body, 'plain', 'utf-8'))
# 添加附件
file_path = 'example.txt'
with open(file_path, 'rb') as f:
attachment = MIMEApplication(f.read(), _subtype='txt')
attachment.add_header('Content-Disposition', 'attachment', filename=('utf-8', '', '示例文件.txt'))
msg.attach(attachment)
# 发送邮件(代码同上)

三、常见错误及解决方案

1. 认证失败错误

错误信息 :smtplib.SMTPAuthenticationError: (535, b'Login Fail. Please enter your authorization code to login.')

解决方案 :

  • 确保用户名和密码正确

  • 对于 QQ、163 等邮箱,需要使用授权码而非登录密码

  • 检查邮箱是否开启了 SMTP 服务

2. 连接错误

错误信息 :smtplib.SMTPServerDisconnected: Connection unexpectedly closed

解决方案 :

  • 检查 SMTP 服务器地址和端口是否正确

  • 确保网络连接正常

  • 尝试使用不同的端口(如 465、587)

3. SSL 错误

错误信息 :ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

解决方案 :

  • 确保 SSL 证书有效

  • 可以临时禁用证书验证(不推荐用于生产环境):

1
2
3
4
5
import ssl
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
server = smtplib.SMTP_SSL(smtp_server, smtp_port, context=context)

4. 邮件被标记为垃圾邮件

解决方案 :

  • 确保邮件内容规范,避免使用垃圾邮件关键词

  • 添加正确的发件人信息和回复地址

  • 配置 SPF、DKIM 等邮件验证机制

  • 避免频繁发送大量邮件

5. 字符编码错误

错误信息 :UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

解决方案 :

  • 确保所有字符串都使用正确的编码(如 UTF-8)

  • 使用 email.header.Header 类处理中文标题和发件人信息

四、进阶用法

1. 使用第三方库

除了标准库外,还有一些第三方库可以简化邮件发送,如 yagmail:

1
2
3
4
5
6
7
8
9
10
11
12
import yagmail
# 配置邮件服务器
yag = yagmail.SMTP(user='your_email@qq.com', password='your_password', host='smtp.qq.com')
# 发送邮件
yag.send(
to='recipient@example.com',
subject='yagmail测试',
contents='这是使用yagmail发送的测试邮件!',
attachments='example.txt'
)
# 关闭连接
yag.close()

2. 发送带图片的 HTML 邮件

在 HTML 邮件中嵌入图片需要使用 email.mime.image.MIMEImage 类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from email.mime.image import MIMEImage
# 添加图片到邮件
with open('image.jpg', 'rb') as f:
img = MIMEImage(f.read())
img.add_header('Content-ID', '<image1>')
msg.attach(img)
# 在HTML中引用图片
html_body = f"""
<html>
<body>
<h1>带图片的HTML邮件</h1>
<p>这是一张图片:</p>
<img src="cid:image1" alt="测试图片">
</body>
</html>
"""

3. 批量发送邮件

对于需要批量发送邮件的场景,可以使用循环遍历收件人列表:

1
2
3
4
5
6
7
8
9
# 收件人列表
recipients = ['user1@example.com', 'user2@example.com', 'user3@example.com']
# 批量发送
for recipient in recipients:
try:
server.sendmail(username, [recipient], msg.as_string())
print(f'向 {recipient} 发送邮件成功!')
except Exception as e:
print(f'向 {recipient} 发送邮件失败:{e}')

五、总结

通过 Python 调用邮件服务是一项实用的技能,可以帮助我们实现各种自动化邮件发送需求。本文介绍了使用 Python 标准库 smtplib 和 email 发送邮件的方法,包括基本文本邮件、HTML 邮件和带附件的邮件,并详细说明了常见错误及解决方案。

在实际应用中,需要根据具体需求选择合适的发送方式,并注意邮件服务器的配置和安全设置。同时,为了提高邮件的送达率,还需要注意邮件内容的规范和反垃圾邮件策略。

希望本文能够帮助你掌握 Python 调用邮件服务的技能,为你的项目开发提供便利!

六、参考资料

  1. Python 官方文档 - smtplib

  2. Python 官方文档 - email

  3. QQ 邮箱 SMTP 服务设置

  4. 163 邮箱 SMTP 服务设置

前言

到今天,作者已经学习编程四年有余,那么我认为写代码的最好帮手是什么?

这就是我今天所要讲的内容。

Top1

Pycharm

不得不说,对于想要学习 python 的新手来说,pycharm 是一个不错的工具,它作为 JetBrains 的优秀产品,集成了代码补全、mysql 数据库等实用功能,可以说是非常强大,并且专业版还有一个月的免费试用

Top2

VSCode

VsCode 具有丰富的插件,在原有的功能下可以有更多的拓展,创造出了更多可能,并且其支持多种的语言编写,适合需要多个语言频繁切换的人使用。

Top 3

IDEA

IntelliJ IDEA 使用工具帮助您更快地编写代码,这些工具可以消除繁琐的任务,让您专注于重要的事情 - 构建出色的软件。

获取编写高质量代码、最大限度地减少错误和减少技术债务所需的一切,以便您可以将更多时间花在创新而不是故障排除上。

它专为极致舒适而设计,提供流畅、愉快的工作流程,帮助您保持流畅。

1. ERROR Deployer not found: git

出现于 hexo clean && hexo g -d

原因分析

在初次部署 hexo 时没有安装 hexo-deployer-git 插件导致的

解决方案

1
npm install hexo-deployer-git --save

2. INFO Validating config

出现于 hexo d

原因分析

与远程部署 hexo 发现远程仓库未更新

解决方案

在使用 hexo d 前,先执行下列命令

1
npm un hexo-deployer-git
1
npm i hexojs/hexo-deployer-git

这样会导致原本的 hexo-deployer-git 插件会出现问题,需要重新安装一遍才能解决问题。

来源:https://blog.csdn.net/YunxBao/article/details/139754089

正在更新中…

0%