Python 字符串和字节串的区别

区别

  • 字符串采用 Unicode 进行编码,一个字符占 1-4 个字节。
    • UTF-8: 可变长度编码,每个字符占用 1 到 4 个字节。
    • UTF-16: 每个字符占用 2 或 4 个字节。
    • UTF-32: 每个字符固定占用 4 个字节。
  • 字节串采用 ACSII 进行编码,一个字符占一字节。

定义

定义字符串对象

s = u'hello,world'
s = 'hello,world' # 默认为字符串

定义字节串对象

s = b'hello, world'

转换方式

  • 从字节串到字符串, 使用 encode 方法
# 字符串编码为字节串
s = "hello, world"
b = s.encode()  # 默认使用 'utf-8' 编码
print(b)  # 输出: b'hello, world'
  • 从字符串到字节串,使用 decode 方法
# 字节串解码为字符串
b = b"hello, world"
s = b.decode()  # 默认使用 'utf-8' 编码
print(s)  # 输出: hello, world

对读写文件的影响

一般情况下,只要文件中的字符可以用 ASCII 码表示,不会产生影响。

读取文件

当从文件中读取数据时,如果文件是以文本模式打开的,Python 会自动进行解码。默认情况下,使用系统默认编码(通常是 utf-8)。

# 读取文件内容
with open('example.txt', 'r', encoding='utf-8') as f:
    content = f.read()
    print(content)
  • 如果文件使用的编码与 open 函数的 encoding 参数不匹配,会导致 UnicodeDecodeError
  • 使用默认的 ‘strict’ 错误处理模式,如果文件包含无法解码的字节,会抛出异常。

可以通过指定 errors 参数来处理解码错误:

with open('example.txt', 'r', encoding='utf-8', errors='ignore') as f:
    content = f.read()
    print(content)  # 忽略无法解码的字节

with open('example.txt', 'r', encoding='utf-8', errors='replace') as f:
    content = f.read()
    print(content)  # 将无法解码的字节替换为 `�`

【注】查看某个文件的编码,可以使用 file 命令:

$file example.txt 
example.txt: Unicode text, UTF-8 text, with no line terminators

写入文件

当向文件中写入数据时,如果文件是以文本模式打开的,Python 会自动进行编码。默认情况下,使用系统默认编码(通常是 utf-8)。

# 写入文件内容
with open('example.txt', 'w', encoding='utf-8') as f:
    f.write("hello, world")

示例代码

读取文件

# 创建一个包含特殊字符的文件
with open('example.txt', 'w', encoding='utf-8') as f:
    f.write("hello, world 😊")

# 读取文件内容,默认使用 utf-8 编码
with open('example.txt', 'r', encoding='utf-8') as f:
    content = f.read()
    print(content)  # 输出: hello, world 😊

# 读取文件内容,使用错误处理模式 'ignore'
with open('example.txt', 'r', encoding='ascii', errors='ignore') as f:
    content = f.read()
    print(content)  # 输出: hello, world 

# 读取文件内容,使用错误处理模式 'replace'
with open('example.txt', 'r', encoding='ascii', errors='replace') as f:
    content = f.read()
    print(content)  # 输出: hello, world ?

写入文件

# 尝试写入无法编码的字符,使用 ascii 编码和 'ignore' 错误处理
with open('example.txt', 'w', encoding='ascii', errors='ignore') as f:
    f.write("hello, world 😊")  # 忽略无法编码的字符
with open('example.txt', 'r', encoding='ascii') as f:
    print(f.read())  # 输出: hello, world 

# 尝试写入无法编码的字符,使用 ascii 编码和 'replace' 错误处理
with open('example.txt', 'w', encoding='ascii', errors='replace') as f:
    f.write("hello, world 😊")  # 将无法编码的字符替换为 '?'
with open('example.txt', 'r', encoding='ascii') as f:
    print(f.read())  # 输出: hello, world ?

总结

  • 读取文件: 默认使用 utf-8 编码。确保文件编码与指定编码一致,使用 errors 参数处理解码错误。
  • 写入文件: 默认使用 utf-8 编码。确保写入的字符在指定编码中可表示,使用 errors 参数处理编码错误。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值