在用python写lmdb文件时出现了报错信息"Won't implicitly convert Unicode to bytes; use .encode()
原报错代码:
def writeCache(env, cache):
with env.begin(write=True) as txn:
for k, v in cache.items():
txn.put(k, v)
修改后的代码:
def writeCache(env, cache):
with env.begin(write=True) as txn:
for k, v in cache.items():
txn.put(str(k).encode(), str(v).encode())
本文介绍了一种在使用Python编写LMDB文件时遇到的Unicode转换错误,并提供了一个简单的解决方案,通过将Unicode字符串编码为字节来避免错误。
1万+

被折叠的 条评论
为什么被折叠?



