python-3.x - python format()结合join和数据转换
问题描述
这个 Python 代码正是我想要的:
a_hex_str = ':'.join('%02x' % b for b in a_bytes)
它是以十六进制打印的字节数组 a_bytes 中的字节值的字符串,每个值之间有一个冒号。
我一直在疯狂尝试使用新的 format() 函数代替旧的 % 来获得相同的输出,但我所有的尝试都给出了错误。
我将非常感谢您的帮助。
解决方案
a_hex_str = ':'.join('{}02x'.format(b) for b in a_bytes)
试试这条线,希望它能奏效。
推荐阅读
- python - 我收到此代码显示 TypeError: '>=' not supported between 'str' 和 'int' 实例
- powershell - 如何评估 PowerShell 中是否返回数据或错误?
- visual-studio - 无法在 Visual Studio 2019 上为 tizen .NET 与三星 Galaxy 建立调试连接
- android - Kotlin Coroutine GlobalScope 文档清晰
- javascript - 有人可以在这里解释“push”是怎么回事吗?
- node.js - 带有 rxjs 的猫鼬 - 在 promise 解决时无法捕获错误
- javascript - 使用 React Hooks 时 Reactjs 中的无限循环错误
- opencv - 在 Heroku 中访问用户的网络摄像头
- visual-studio - 用于删除具有命名约定的某些文件的 Visual Studio 预构建事件
- rust - 如何在 Rust 中不使用 BLAS 后端的情况下使用 ndarray 进行矩阵求逆?