“TypeError:没有编码的字符串参数”,但字符串是否被编码? [英] "TypeError: string argument without an encoding", but the string is encoded?

查看:3485
本文介绍了“TypeError:没有编码的字符串参数”,但字符串是否被编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Python2转换现有程序到Python3。程序中的一种方法使用远程服务器验证用户。它将提示用户输入密码。

  def _handshake(self):
timestamp = int .time())
token =(md5hash(md5hash((self.password).encode('utf-8'))hexdigest()
+ str(bytes('timestamp' ('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(\\\

if lines [0]!=OK:
raise ScrobbleException(Server returned:%s%(response,))
self.session_id = lines [1]
self.submit_url = lines [3]

问题wi这个方法是将整数转换为字符串后,需要进行编码。但据我所知,它已经被编码了?我发现这个问题,但是我很难将它应用于上下文的这个程序。



这是给我问题的线。




  • code> + str(bytes('timestamp')。encode('utf-8'))))


    • TypeError:没有编码的字符串参数




我已经尝试着使用其他方式来执行此操作,所有这些都有不同类型的错误。




  • + str(bytes('timestamp','utf-8'))))


    • TypeError:Unicode对象必须在散列之前编码


  • + str ('timestamp','utf-8')))


    • TypeError:支持




我还在开始学习g Python(但我已经初学Java中间知识),所以我还不完全熟悉这种语言。有没有人对这个问题有什么想法?



谢谢!

解决方案>

此错误是由于您在python 3中创建字节。



您不会执行字节(bla bla),但只需 bblabla,或者您需要指定和编码类型,如 bytes(bla bla,utf- 8),因为它需要知道原始编码是什么,然后把它变成一个数组。



然后错误

  TypeError:没有编码的字符串参数

应该消失。



你有两个字节或str。如果你有一个字节值,并且你想把它转换成str你应该做:

  my_bytes_value.decode(utf-8 )

它会返回一个str。



我希望它有帮助!祝你今天愉快 !


I am working on converting an existing program from 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.

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 question but 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

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

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?

Thanks!

解决方案

This error is due to how you create bytes in python 3.

You will not do bytes("bla bla") but just b"blabla" or you need to specify and 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.

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:

my_bytes_value.decode("utf-8")

And it will return you a str.

I hoped it help ! Have a nice day !

这篇关于“TypeError:没有编码的字符串参数”,但字符串是否被编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆