python - 字符串转换和编码
问题描述
我在数据库中有一个字符串,它是一个编码字符串。如何将其更改为字节数据类型,以便使用 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='"
我该如何解决这个问题?另外,如何调用存储在字符串中的字节数据类型而不会出现任何错误?
解决方案
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()
推荐阅读
- python-3.x - 运行 python 代码会消耗 GPU。为什么?
- python - 烧瓶登录(无数据库)
- testing - dqn 的训练部分以及与 SVR 和 RF 的比较
- javascript - 错误 TS1005: ',' 预期,编译 Angular 6 项目失败
- android - 如何从 DialogFlowV2 webhook 发送不同的语音和显示文本?
- apache-spark - 无法启动 spark-shell
- php - 如何在本地主机上运行 IXP 管理器
- highcharts - 如何使用填充自定义热图单元?
- dji-sdk - DJI Spark - 自主飞行?
- c# - SQL查询到mvc中的linq