python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python字节串“b“前缀

Python字节串"b"前缀案例解析

作者:追逐此刻

文章主要介绍了Python中的字节串(bytes)类型,包括其定义、特性、应用场景、语法规则、类型转换方法以及实际应用案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

核心结论:前缀"b"用于创建字节串对象,它以二进制形式存储数据,是Python处理文件I/O、网络通信等底层操作的基石。

一、基础定义

官方名称:字节串

核心功能:表示不可变的二进制数据序列(0-255的整数序列)。

语法形式:在字符串引号前加"b"或"B"

b'hello'  # 字节串
b"world"  # 同上
B'test'   # 同上

二、数据类型特性对比

维度普通字符串 (str)字节串
存储内容Unicode字符原始字节数据(0-255)
长度单位字符数字节数
编码已经解码,无需编码需要指定编码才能转为文本
可打印性直接显示字符显示转义形式(如b'\xe4\xb8\xad')
不可变性不可变不可变
类型检查type('') is strtype(b'') is bytes

本质区别示例

s = '中'      # 长度1,包含一个Unicode字符
b = b'\xe4\xb8\xad'  # 长度3,包含3个字节(UTF-8编码)
print(len(s))  # 1
print(len(b))  # 3

三、必须使用字节串的三大典型场景

1.文件I/O(二进制模式)

读写图片、音频、视频、压缩文件等非文本文件时,必须用字节串:

with open('image.jpg', 'rb') as f:  # 注意'rb'表示二进制读
    data = f.read()  # 返回bytes对象
with open('copy.jpg', 'wb') as f:
    f.write(data)

2.网络通信(socket编程)

网络协议传输的是原始字节流,而非文本:

import socket
sock = socket.socket()
sock.connect(('example.com', 80))
request = b'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n'
sock.sendall(request)  # 必须发送bytes
response = sock.recv(4096)  # 返回bytes

3.加密与哈希运算

加密算法处理的是二进制数据:

import hashlib
md5 = hashlib.md5()
md5.update(b'secret')  # 必须传入bytes
print(md5.hexdigest())

四、语法规则

正确声明方式

b'hello'        # 单引号
b"world"        # 双引号
b'''multi'''    # 三单引号
B"""multi"""    # 三双引号
b'\xe4\xb8\xad' # 转义序列

常见错误写法

  1. 混合使用编码字符
b'中文字符'  # ❌ SyntaxError: bytes can only contain ASCII literal
b'\xe4\xb8\xad'  # ✅ 正确:用十六进制表示UTF-8编码
  1. 字符串拼接错误
'text' + b'bytes'  # ❌ TypeError: can't concat str to bytes
b'text' + b'bytes'  # ✅ 正确
  1. 格式化不支持
b'hello {}'.format('world')  # ❌ 字节串不支持.format()

五、类型转换方法

str → bytes(编码)

# 方法1:encode()
text = '你好世界'
byte_data = text.encode('utf-8')  # b'\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c'
# 方法2:bytes构造函数
byte_data = bytes(text, 'utf-8')
byte_data = bytes(text, encoding='utf-8')
# 方法3:bytes字面量(仅限ASCII)
byte_data = b'hello'

bytes → str(解码

# 方法1:decode()
text = byte_data.decode('utf-8')  # '你好世界'
# 方法2:str构造函数
text = str(byte_data, 'utf-8')
text = str(byte_data, encoding='utf-8')
# 处理解码错误
text = byte_data.decode('utf-8', errors='ignore')  # 忽略错误字符
text = byte_data.decode('utf-8', errors='replace')  # 用�替换

六、操作限制与替代方案

字节串不支持的字符串操作

b'hello'.upper()    # ✅ 支持(仅ASCII)
b'hello'.lower()    # ✅ 支持(仅ASCII)
b'hello'.strip()    # ✅ 支持
# ❌ 以下操作不支持或不按预期工作:
b'hello {}'.format('world')  # 不支持格式化
b'hello' + 'world'          # 不能与str拼接
b'hello'.isalpha()          # 非ASCII字符行为不同

替代方案

  1. 需要高级字符串操作?先解码
byte_data = b'hello \xe4\xb8\xad'
text = byte_data.decode('utf-8')
text = text.upper()  # 使用str的方法
byte_result = text.encode('utf-8')
  1. 字节串专用方法
b'hello'.find(b'e')      # ✅ 支持
b'hello'.replace(b'l', b'x')  # ✅ 支持
b'hello'.split(b'e')     # ✅ 支持
b'abc'.hex()             # ✅ 转为十六进制字符串
bytes.fromhex('616263')  # ✅ 从十六进制创建

七、实际应用案例

案例1:二进制文件复制器

def copy_binary_file(src, dst, chunk_size=8192):
    """高效复制二进制文件"""
    with open(src, 'rb') as f_src, open(dst, 'wb') as f_dst:
        while True:
            chunk = f_src.read(chunk_size)  # 读取bytes块
            if not chunk:
                break
            f_dst.write(chunk)
# 使用示例
copy_binary_file('original.jpg', 'backup.jpg')

案例2:HTTP请求客户端

import socket
def simple_http_request(host, path='/'):
    """发送简单的HTTP GET请求"""
    # 构建HTTP请求(必须是bytes)
    request = f"GET {path} HTTP/1.1\r\nHost: {host}\r\n\r\n"
    request_bytes = request.encode('utf-8')
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, 80))
    try:
        sock.sendall(request_bytes)
        response = b''
        while True:
            chunk = sock.recv(4096)
            if not chunk:
                break
            response += chunk
        # 分离头部和主体
        header, _, body = response.partition(b'\r\n\r\n')
        print("Response Header:")
        print(header.decode('utf-8', errors='ignore'))
        return body
    finally:
        sock.close()
# 使用示例
content = simple_http_request('example.com', '/')

案例3:文件哈希校验工具

import hashlib
def calculate_file_hash(filepath, algorithm='md5'):
    """计算文件的哈希值"""
    hash_func = hashlib.new(algorithm)
    with open(filepath, 'rb') as f:  # 二进制模式读取
        while True:
            chunk = f.read(8192)  # 分块读取bytes
            if not chunk:
                break
            hash_func.update(chunk)
    return hash_func.hexdigest()
# 使用示例
print(f"MD5: {calculate_file_hash('document.pdf', 'md5')}")
print(f"SHA256: {calculate_file_hash('document.pdf', 'sha256')}")

关键要点总结

  1. 记住黄金法则:涉及二进制数据(文件/网络/加密)时,用bytes;涉及文本处理时,用str
  2. 编码解码桥梁.encode() 将 str 转为 bytes,.decode() 将 bytes 转为 str
  3. 错误处理:解码时始终考虑 errors 参数(ignore/replace
  4. 性能考虑:大文件操作时,分块读写bytes更高效

到此这篇关于Python字节串“b“前缀案例解析的文章就介绍到这了,更多相关Python字节串“b“前缀内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文