Problem with code compatibility with Python 3.9 - "TypeError: a bytes-like object is required, not 'str'"
Problem with code compatibility with Python 3.9 - "TypeError: a bytes-like object is required, not 'str'"
在 运行 Python 3.9 上尝试 运行 遥测 python 应用程序时,失败并显示错误 "TypeError: a bytes-like object is required,不是 'str'"
尝试按照其他地方的建议将 'r' 更改为 'rb'(并将 'w' 更改为 'wb')来修复代码会导致不同的错误。
不幸的是,我无法弄清楚这一点。谁能帮我找出这里的问题?我对此很陌生。提前致谢。
def __init__(self):
try:
with open(STATUS_FILE, 'r') as sfd:
self.st_data = pickle.loads(sfd.read())
except IOError:
self.st_data = dict(seq=0, timestamp=int(time.time()), rx_packets=0, tx_packets=0)
self.st_data['seq'] = (self.st_data['seq'] % 999) + 1
def __repr__(self):
return "%s %s" % (self.__class__, self.st_data)
def save(self):
self.st_data['timestamp'] = int(time.time())
try:
with open(STATUS_FILE, 'w') as sfd:
sfd.write(pickle.dumps(self.st_data))
except IOError as err:
print(err)
pickle.loads() 将 bytes-like 对象作为第一个参数。你正在发送一个字符串。参见:https://docs.python.org/3/library/pickle.html#pickle.loads
此处更深入地介绍了将字符串转换为字节:Best way to convert string to bytes in Python 3?
对您来说最简单的方法可能是:
my_data = bytes(sfd.read(), 'utf-8')
使用二进制文件模式是正确的做法。通过这些更改使您的代码成为可重现的示例。确保删除由原始 non-working 代码创建的任何 STATUS_FILE:
STATUS_FILE = 'test.pkl' # defined missing variable
class Test: # added shown methods to a class
def __init__(self):
try:
with open(STATUS_FILE, 'rb') as sfd: # use binary mode
self.st_data = pickle.loads(sfd.read())
except IOError:
self.st_data = dict(seq=0, timestamp=int(time.time()), rx_packets=0, tx_packets=0)
self.st_data['seq'] = (self.st_data['seq'] % 999) + 1
def __repr__(self):
return "%s %s" % (self.__class__, self.st_data)
def save(self):
self.st_data['timestamp'] = int(time.time())
try:
with open(STATUS_FILE, 'wb') as sfd: # use binary mode
sfd.write(pickle.dumps(self.st_data))
except IOError as err:
print(err)
t = Test() # instantiate class, read from save file if present
print(t) # view the result
t.save() # create the save file
多次运行的输出。序列增量和时间戳变化。
<class '__main__.Test'> {'seq': 1, 'timestamp': 1644104029, 'rx_packets': 0, 'tx_packets': 0}
<class '__main__.Test'> {'seq': 2, 'timestamp': 1644104029, 'rx_packets': 0, 'tx_packets': 0}
<class '__main__.Test'> {'seq': 3, 'timestamp': 1644104030, 'rx_packets': 0, 'tx_packets': 0}
<class '__main__.Test'> {'seq': 4, 'timestamp': 1644104030, 'rx_packets': 0, 'tx_packets': 0}
<class '__main__.Test'> {'seq': 5, 'timestamp': 1644104031, 'rx_packets': 0, 'tx_packets': 0}
在 运行 Python 3.9 上尝试 运行 遥测 python 应用程序时,失败并显示错误 "TypeError: a bytes-like object is required,不是 'str'"
尝试按照其他地方的建议将 'r' 更改为 'rb'(并将 'w' 更改为 'wb')来修复代码会导致不同的错误。
不幸的是,我无法弄清楚这一点。谁能帮我找出这里的问题?我对此很陌生。提前致谢。
def __init__(self):
try:
with open(STATUS_FILE, 'r') as sfd:
self.st_data = pickle.loads(sfd.read())
except IOError:
self.st_data = dict(seq=0, timestamp=int(time.time()), rx_packets=0, tx_packets=0)
self.st_data['seq'] = (self.st_data['seq'] % 999) + 1
def __repr__(self):
return "%s %s" % (self.__class__, self.st_data)
def save(self):
self.st_data['timestamp'] = int(time.time())
try:
with open(STATUS_FILE, 'w') as sfd:
sfd.write(pickle.dumps(self.st_data))
except IOError as err:
print(err)
pickle.loads() 将 bytes-like 对象作为第一个参数。你正在发送一个字符串。参见:https://docs.python.org/3/library/pickle.html#pickle.loads
此处更深入地介绍了将字符串转换为字节:Best way to convert string to bytes in Python 3?
对您来说最简单的方法可能是:
my_data = bytes(sfd.read(), 'utf-8')
使用二进制文件模式是正确的做法。通过这些更改使您的代码成为可重现的示例。确保删除由原始 non-working 代码创建的任何 STATUS_FILE:
STATUS_FILE = 'test.pkl' # defined missing variable
class Test: # added shown methods to a class
def __init__(self):
try:
with open(STATUS_FILE, 'rb') as sfd: # use binary mode
self.st_data = pickle.loads(sfd.read())
except IOError:
self.st_data = dict(seq=0, timestamp=int(time.time()), rx_packets=0, tx_packets=0)
self.st_data['seq'] = (self.st_data['seq'] % 999) + 1
def __repr__(self):
return "%s %s" % (self.__class__, self.st_data)
def save(self):
self.st_data['timestamp'] = int(time.time())
try:
with open(STATUS_FILE, 'wb') as sfd: # use binary mode
sfd.write(pickle.dumps(self.st_data))
except IOError as err:
print(err)
t = Test() # instantiate class, read from save file if present
print(t) # view the result
t.save() # create the save file
多次运行的输出。序列增量和时间戳变化。
<class '__main__.Test'> {'seq': 1, 'timestamp': 1644104029, 'rx_packets': 0, 'tx_packets': 0}
<class '__main__.Test'> {'seq': 2, 'timestamp': 1644104029, 'rx_packets': 0, 'tx_packets': 0}
<class '__main__.Test'> {'seq': 3, 'timestamp': 1644104030, 'rx_packets': 0, 'tx_packets': 0}
<class '__main__.Test'> {'seq': 4, 'timestamp': 1644104030, 'rx_packets': 0, 'tx_packets': 0}
<class '__main__.Test'> {'seq': 5, 'timestamp': 1644104031, 'rx_packets': 0, 'tx_packets': 0}