简体   繁体   English

如何在 python 中编码为 Base64?

[英]How to Encode to Base64 in python?

Hey I am trying to encode something to base64 but I am getting an invalid format error I have no idea why maybe someone can help me:)嘿,我正在尝试将某些内容编码为 base64 但我收到无效格式错误我不知道为什么有人可以帮助我:)

creqBytes = bytes(f'{"threeDSServerTransID":threeDSServerTransID,"transStatus":"Y","acsTransID":acsTransID,"messageType":"CRes","messageVersion":"2.1.0"}', 'utf-8')
creq2 = base64.b64encode(creqBytes).decode("ascii")
print(creq2)

The ValueError: Invalid format specifier error message you are getting is from your first line. ValueError: Invalid format specifier错误消息来自您的第一行。 This is because you have an f before the string indicates that this will be a string with f-string formatting in it.这是因为在字符串之前有一个f表示这将是一个带有f 字符串格式的字符串 However, you haven't put any f-string formatting in the string.但是,您没有在字符串中放置任何 f 字符串格式。 Hence the error message.因此出现错误消息。

I was able to get this to run by removing the f .我能够通过删除f来运行它。 Here is my test:这是我的测试:

import base64

creqBytes = bytes('{"threeDSServerTransID":threeDSServerTransID,"transStatus":"Y","acsTransID":acsTransID,"messageType":"CRes","messageVersion":"2.1.0"}', 'utf-8')
print(creqBytes)
# b'{"threeDSServerTransID":threeDSServerTransID,"transStatus":"Y","acsTransID":acsTransID,"messageType":"CRes","messageVersion":"2.1.0"}'

creq2 = base64.b64encode(creqBytes).decode("ascii")
print(creq2)
# eyJ0aHJlZURTU2VydmVyVHJhbnNJRCI6dGhyZWVEU1NlcnZlclRyYW5zSUQsInRyYW5zU3RhdHVzIjoiWSIsImFjc1RyYW5zSUQiOmFjc1RyYW5zSUQsIm1lc3NhZ2VUeXBlIjoiQ1JlcyIsIm1lc3NhZ2VWZXJzaW9uIjoiMi4xLjAifQ==

However, I suspect that isn't what you are trying to do.但是,我怀疑这不是您想要做的。 My guess would be that you have some variables in Python that you want to include in the string.我的猜测是您想在字符串中包含 Python 中的一些变量。 If that is the case then the f-string would look like:如果是这种情况,那么 f 字符串将如下所示:

import base64

threeDSServerTransID = 2
acsTransID = 3

creqBytes = bytes(f'{{"threeDSServerTransID":{threeDSServerTransID},"transStatus":"Y","acsTransID":{acsTransID},"messageType":"CRes","messageVersion":"2.1.0"}}', 'utf-8')
print(creqBytes)
# b'{"threeDSServerTransID":2,"transStatus":"Y","acsTransID":3,"messageType":"CRes","messageVersion":"2.1.0"}'
creq2 = base64.b64encode(creqBytes).decode("ascii")
print(creq2)
# eyJ0aHJlZURTU2VydmVyVHJhbnNJRCI6MiwidHJhbnNTdGF0dXMiOiJZIiwiYWNzVHJhbnNJRCI6MywibWVzc2FnZVR5cGUiOiJDUmVzIiwibWVzc2FnZVZlcnNpb24iOiIyLjEuMCJ9

That formatting of the f-string looks like it would be error prone and difficult for other people to read. f 字符串的这种格式看起来容易出错并且其他人难以阅读。 I suspect that having the data as a Python dictionary and outputting it to a JSON format before then doing base64 encoding might be easier to understand and read.我怀疑将数据作为 Python 字典并将其输出为 JSON 格式,然后再进行 base64 编码可能更容易理解和阅读。 This would look like:这看起来像:

import base64
import json

threeDSServerTransID = 2
acsTransID = 3

var1 = {
    "threeDSServerTransID": threeDSServerTransID,
    "transStatus": "Y",
    "acsTransID": acsTransID,
    "messageType": "CRes",
    "messageVersion": "2.1.0"
}
var2 = json.dumps(var1)
print(var2)
# '{"threeDSServerTransID": 2, "transStatus": "Y", "acsTransID": 3, "messageType": "CRes", "messageVersion": "2.1.0"}'

var3 = base64.b64encode(var2.encode())
print(var3)
# b'eyJ0aHJlZURTU2VydmVyVHJhbnNJRCI6IDIsICJ0cmFuc1N0YXR1cyI6ICJZIiwgImFjc1RyYW5zSUQiOiAzLCAibWVzc2FnZVR5cGUiOiAiQ1JlcyIsICJtZXNzYWdlVmVyc2lvbiI6ICIyLjEuMCJ9'
print(var3.decode('ascii'))
# eyJ0aHJlZURTU2VydmVyVHJhbnNJRCI6IDIsICJ0cmFuc1N0YXR1cyI6ICJZIiwgImFjc1RyYW5zSUQiOiAzLCAibWVzc2FnZVR5cGUiOiAiQ1JlcyIsICJtZXNzYWdlVmVyc2lvbiI6ICIyLjEuMCJ9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM