TypeError: byte indices must be integers or slices, not str
时间: 2023-10-13 19:25:02 浏览: 302
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
5星 · 资源好评率100%
This error occurs when you try to access a byte object using a string index. Byte objects are sequences of bytes, and their elements can only be accessed using integer indices or slices.
For example:
```
my_bytes = b"hello"
print(my_bytes[0]) # This will output: 104
print(my_bytes[1:3]) # This will output: b"el"
print(my_bytes["h"]) # This will raise a TypeError
```
In the last line of the code, we are trying to access a byte object using a string index ("h"), which is not allowed. To fix this error, you need to use integer indices or slices when accessing byte objects.
阅读全文