0

我想使用 Base64 将一个整数编码为一个短字符串并将该值返回给 Open Refine (Google Refine)。

我找到了一些例子,但它们总是给我一个错误。

import base64
foo = base64.b64encode('1')
return foo

作品返回“MQ==”

但我想对整数 1 进行编码。下面的代码给了我一个错误。

import base64
foo = base64.b64encode(bytes([1]))
return foo

我找到的例子在这里:How to encode integer in to base64 string in python 3

4

1 回答 1

1

您可以以字节的十六进制表示形式使用字符串中的二进制数据\xxxx

使用 Python2(包括 Jython2)尝试:

foo = base64.b64encode('\01')

对于 Python3:

foo = base64.b64encode(b'\01')

我的结果:AQ==

于 2015-04-15T10:58:16.607 回答