【发布时间】:2021-03-11 09:59:30
【问题描述】:
我正在从 python 2 更新我的代码,但我遇到了以前在该版本中有效的错误。我很迷茫,请帮忙。
我在这里遇到了一个错误,除了函数:
for c in text:
try:
if isEncrypt:
i = tp_alphabet.index(c)
ret += tc_alphabet[i]
else:
i = tc_alphabet.index(c)
ret += tp_alphabet[i]
except ValueError as e:
wrchar = c.encode('utf-8')
raise Exception("Can't find char '" + wrchar + "' of text in alphabet!")
当我运行这个时:
dec = machine.decrypt(plaintext)
print(dec)
这是错误:
File "python3.py", line 133, in __encDec
raise Exception("Can't find char '" + wrchar + "' of text in alphabet!")
TypeError: can only concatenate str (not "bytes") to str
【问题讨论】:
-
用您自己的话来说,您认为
wrchar = c.encode('utf-8')会做什么?为什么你的代码中有这一行? -
@TheLazyScripter 我不认为这是一个很好的重复选择。它有点解释了这个问题;但在这种情况下,进行转换是合适的,而在这种情况下,删除不必要的转换更合适。
标签: python python-3.x string typeerror