问题描述
我正在阅读有关字节和字节数组的内容.我读到了字节数组的可变类型!因此,当我尝试对其进行修改时,我收到一条错误消息,提示 integer是必需的
我在这里缺少什么吗?以下是我的代码和错误
I was reading about bytes and byte array's. I read that byte arrays mutable types!so, when i am trying to modify it i am getting an error saying integer is required
Am i missing something here? The following is my code and the error
z=bytearray("hello world","utf-8")
z[0] ="H"
我遇到了以下错误
TypeError:必须为整数
TypeError: an integer is required
推荐答案
文档显示:
将每个字符转换为ASCII 整数值时可以使用字符串创建的原因.因此,当分配'H'
时,实际上是指分配 72
.
The reason you can create it with a string as each character is converted to its ASCII integer value. So when assigning 'H'
you actually mean to assign 72
.
如果您希望能够分配字符,则只需将每个字符首先传递给 ord()
.
If you want to be able to assign chars, then just pass each one into ord()
first.
这篇关于如何修改字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!