Python 使用技巧

类型转换

# 按string来显示,byarray代表接收到的数据
readstr = byarray.decode('utf-8')#这样就直接转换成str格式
 

# 强制转换
readstr = str(byarray)#用这种方式得到的数据会带有b''字符

 
# 将读取的数据按十六进制字符显示,能让我们直接看到最底层的数据格式
readstr = ' '.join(hex(x) for x in byarray)#这句能把byarray里的数据遍历一遍转换成hex格式,而且用空格相连


# string十六进制转bytearray
bytearray.fromhex(sm[i][:4840])


# str 转 bytes (encode)
str="aabbcc"
bytes=str.encode('utf-8')


# bytes 转 str (decode)
bytes=b"aabbcc"
str=bytes.decode('utf-8')


# bytes 转 bytearray
bytes=b"aabbcc"
byarray=bytearray(bytes)


# str 转 bytearray (encoding)
str="aabbcc"
byarray=bytearray(str)


byarray=bytearray("aabbcc",encoding='utf-8')
str=byarray.decode('utf-8')
bytes=bytes(byarray)
print (byarray)
bytearray(b'aabbcc')
print(str)
aabbcc
print(bytes)
b'aabbcc'


# Python生成十六进制数补零的方法
print('{:02x}'.format(j))    # 输出小写: 0e
print('{:02X}'.format(j))    # 输出大写: 0E
print('0x{:02x}'.format(j))  # 加插0x输出小写: 0x0e
print('0x{:02X}'.format(j))  # 加插0x输出大写: 0x0E
print('0x{:08X}'.format(j))  # 加插0x输出八位: 0x0000000E


进度条

Python实现显示进度条_python 显示进度-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/TaoismHuang/article/details/120747536

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuckyHandCode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值