首页 > 解决方案 > 字符串转换和编码

问题描述

我在数据库中有一个字符串,它是一个编码字符串。如何将其更改为字节数据类型,以便使用 python 不会出错。

数据库中的字符串 = 'b''yAe45XvJkMzMkhP_c6NDKe_OzJyxyJPidssdT4yOFSmT-S02iMXn8_RuC4EsPu9EiPE9KuXLUpR1CIsOnTrfKjmqjYz5rOG8Fplr6NUbFk2sdsdtrPDq_SpV7vg='''

Code : 
encMessage1 = df['data_value'][4] #calling string from MYSQL database
encMessage1 = encMessage1.encode()
cont = fernet.decrypt(encMessage1).decode()

它在字符串中显示错误,并且输出未正确显示

错误:cryptography.fernet.InvalidToken

使用 encode() 后的错误字符串 - b"b'yAe45XvJkMzMkhP_c6NDKe_OzJyxyJPidssdT4yOFSmT-S02iMXn8_RuC4EsPu9EiPE9KuXLUpR1CIsOnTrfKjmqjYz5rOG8Fplr6NUbFk2sdsdtrPDq_SpV7vg='"

我该如何解决这个问题?另外,如何调用存储在字符串中的字节数据类型而不会出现任何错误?

标签: pythonencodingpython-cryptography

解决方案


Based on your response to my question in the comments, the data is being stored with the bytes repr marker.

The solution is to chop off the bytes marker:

enc_message1 = enc_message1[2:-1]

Then use .encode()


推荐阅读