Python “TypeError:没有编码的字符串参数”,但字符串已编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37601804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"TypeError: string argument without an encoding", but the string is encoded?
提问by Justin
I am working on converting an existing programfrom Python2 to Python3. One of the methods in the program authenticates the user with a remote server. It will prompt the user to enter in a password.
我正在将现有程序从 Python2转换为 Python3。程序中的一种方法是通过远程服务器对用户进行身份验证。它将提示用户输入密码。
def _handshake(self):
timestamp = int(time.time())
token = (md5hash(md5hash((self.password).encode('utf-8')).hexdigest()
+ str(bytes('timestamp').encode('utf-8'))))
auth_url = "%s/?hs=true&p=1.2&u=%s&t=%d&a=%s&c=%s" % (self.name,
self.username,
timestamp,
token,
self.client_code)
response = urlopen(auth_url).read()
lines = response.split("\n")
if lines[0] != "OK":
raise ScrobbleException("Server returned: %s" % (response,))
self.session_id = lines[1]
self.submit_url = lines[3]
The problem with this method is that after the integer is converted to a string, it needs to be encoded. But as far as I can tell, it is already encoded? I found this questionbut I was having a hard time applying that to the context of this program.
这种方法的问题在于,整数转换为字符串后,需要对其进行编码。但据我所知,它已经被编码了?我发现了这个问题,但我很难将它应用到这个程序的上下文中。
This is the line giving me problems.
这是给我带来问题的线路。
+ str(bytes('timestamp').encode('utf-8'))))
TypeError: string argument without an encoding
+ str(bytes('timestamp').encode('utf-8'))))
TypeError: string argument without an encoding
I have tried playing around with alternate ways of doing this, all with varying types of errors.
我尝试过使用其他方法来执行此操作,但都存在不同类型的错误。
+ str(bytes('timestamp', 'utf-8'))))
TypeError: Unicode-objects must be encoded before hashing
+ str('timestamp', 'utf-8')))
TypeError: decoding str is not supported
+ str(bytes('timestamp', 'utf-8'))))
TypeError: Unicode-objects must be encoded before hashing
+ str('timestamp', 'utf-8')))
TypeError: decoding str is not supported
I'm still getting started learning Python (but I have beginner to intermediate knowledge of Java), so I am not completely familiar with the language yet. Does anyone have any thoughts on what this issue might be?
我仍在开始学习 Python(但我有 Java 的初级到中级知识),所以我还没有完全熟悉这门语言。有没有人对这个问题可能有什么想法?
Thanks!
谢谢!
回答by Oekhny
This error is due to how you create bytes in python 3.
此错误是由于您在 python 3 中创建字节的方式造成的。
You will not do bytes("bla bla")
but just b"blabla"
or you need to specify an encoding type like bytes("bla bla","utf-8")
because it needs to know what was the original encoding before turning it into an array of numbers.
您不会这样做,bytes("bla bla")
而只是b"blabla"
或者您需要指定一种编码类型,bytes("bla bla","utf-8")
因为它需要在将其转换为数字数组之前知道原始编码是什么。
Then the error
然后错误
TypeError: string argument without an encoding
Should disappear.
应该消失。
You have either bytes or str. If you have a bytes value and you want to turn it in str you should do:
您有字节或字符串。如果您有一个 bytes 值并且想将其转换为 str ,则应该执行以下操作:
my_bytes_value.decode("utf-8")
And it will return you a str.
它会返回一个str。
I hoped it help ! Have a nice day !
我希望它有帮助!祝你今天过得愉快 !