byte indices must be integers or slices, not str
时间: 2023-10-12 16:18:26 浏览: 200
This error occurs when you try to use a string as an index for a byte array or bytes object. Byte indices must be integers or slices, not strings.
For example, if you have a bytes object and you try to access a specific byte using a string index:
```
my_bytes = b'hello world'
my_index = '2'
print(my_bytes[my_index])
```
You will get the "TypeError: byte indices must be integers or slices, not str" error because `my_index` is a string and not an integer.
To fix this error, make sure that you are using integer indices when accessing bytes in a bytes object. If you need to convert a string index to an integer, you can use the `int()` function:
```
my_bytes = b'hello world'
my_index = '2'
print(my_bytes[int(my_index)])
```
This will output the byte at index 2, which is the letter "l".
阅读全文