Python中的字符串相关转换

本文介绍了Python 3.X中字符串与字节的转换方法,包括使用int和float进行数值转换,使用%和string.Formatter.format进行字符串输出,以及decode和encode函数在Unicode编码中的应用。还提及了bytes和bytearray的区别,以及Struct类在处理数据格式转换中的作用,特别是struct.unpack和struct.pack。另外,ctypes.create_string_buffer创建的c_char_array对象,其raw属性能直接获取int类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python 3.X 中,默认的字符串str为unicode编码,在普通的使用中基本可以不用考虑。
使用int(str) float(str) 等函数进行字符串到数值的转换。
使用 % 可进行C语言中printf风格的字符串输出,此外,也有Python更为强大的string.Formatter 类其中的format函数
使用举例

>>> print('%(language)s has %(number)03d quote types.' %
...       {'language': "Python", "number": 2})
Python has 002 quote types.
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
'Coordinates: 37.24N, -115.81W'
>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58'
>>> octets = [192, 168, 0, 1]
>>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)
'C0A80001'

希望在str表示与实际字节之间转换时,使用
str = bytes.decode(encoding=”utf-8”, errors=”strict”) 和
bytes = str.encode(encoding=”utf-8”, errors=”strict”) 函数
特别关注此二函数的命名与转换方向


bytes是Python中的字节区,不可更改(immutable),bytearray是可更改版本
为了将其转为int等类型,需要使用Struct类的函数
struct.unpack(fmt, buffer)
其中,fmt是描述数据格式的字符串

SymC TypePython TypeSize
xpad byteno value
ccharbytes of length 11
bsigned charinteger1
Bunsigned charinteger1
?_Boolbool1
iintinteger4
Iunsigned intinteger4
llonginteger4
Lunsigned long integer 4


用法举例如下:

>>> from struct import *
>>> unpack('hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
(1, 2, 3)
>>> calcsize('hhl')
8
>>>a = bytes([1,55,0xab])
>>>print(a)
b'\x017\xab'
>>>struct.unpack('3B',a) 
#也可struct.unpack(str(len(a)) + 'B', a)
(1, 55, 171)

此外还有struct.pack(fmt, v1, v2, …)

>>> pack('hhl', 1, 2, 3)
b'\x00\x01\x00\x02\x00\x00\x00\x03'


注意对齐问题

>>> pack('ci', b'*', 0x12131415)
b'*\x00\x00\x00\x12\x13\x14\x15'
>>> pack('ic', 0x12131415, b'*')
b'\x12\x13\x14\x15*'

与bytes不同的设计,ctypes.create_string_buffer(b, size)函数创建的c_char_array对象,含有的raw对象可以直接用[idx]得到int类型

>>> a = ctypes.create_string_buffer(b'\000',2)
>>> b = a.raw[0]
>>> type(b)
<class 'int'>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值