【问题标题】:How can I encode an integer into Base64 in Jython for Open Refine?如何在 Jython 中将整数编码为 Base64 以进行 Open Refine?
【发布时间】:2015-04-14 16:02:58
【问题描述】:

我想使用 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

【问题讨论】:

    标签: python base64 jython openrefine


    【解决方案1】:

    您可以使用\xx 形式的字符串中的二进制数据,其中xx 是字节的十六进制表示。

    用 Python2(包括 Jython2)试试:

    foo = base64.b64encode('\01')
    

    对于 Python3:

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

    我的结果:AQ==

    【讨论】:

    • 谢谢@Michał。我不熟悉 Python 字节类型。
    猜你喜欢
    • 2014-07-11
    • 2011-09-05
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多