我有一个函数应该读取文件作为字节并剥离换行符,但是当我尝试使用.strip()它给我错误 TypeError: a bytes-like object is required, not 'str'
,所以然后我尝试在剥离前使用 .encode('utf-8')
编码它,我得到 AttributeError: 'bytes' object has no attribute 'encode'
. 我没有't really know where to begin with this problem. Here'的代码:
file = open(str(filename + ".data"), "rb")
file.seek(0)
array = file.readlines()
b = array[lineNumber].strip('\n\r')
该文件是加密字节,我正在尝试将其输入到解密函数中以获取ascii .
1 回答
This comment告诉我,我需要使用带有字节的
.strip()
而不是字符串:.strip(b'\n\r')
而不是.strip('\n\r')
,因为我正在剥离字节 .