1

我有大型二进制 zip 文件作为 JSON 接口的一部分进行传输。为此,我已将它们转换为 base64,但我无法清晰地读取它们,如下面的一个简单案例所示:

~ $ ipython --nobanner

In [1]: epub = 'trial/epubs/9780857863812.epub'

In [2]: import base64

In [3]: import json

In [4]: f = open(epub, 'rb')

In [5]: content = f.read()

In [7]: base64.urlsafe_b64decode(base64.urlsafe_b64encode(content)) == content
Out[7]: True


In [8]: base64.urlsafe_b64decode(json.loads(json.dumps(base64.urlsafe_b64encode(content)))) == content
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/paul/wk/evanidus/repo_wc/branches/services/carroll/dev/<ipython console> in <module>()

/usr/lib/python2.6/base64.pyc in urlsafe_b64decode(s)
    110     The alphabet uses '-' instead of '+' and '_' instead of '/'.
    111     """
--> 112     return b64decode(s, '-_')
    113 
    114 

/usr/lib/python2.6/base64.pyc in b64decode(s, altchars)
     69     """
     70     if altchars is not None:
---> 71         s = _translate(s, {altchars[0]: '+', altchars[1]: '/'})
     72     try:
     73         return binascii.a2b_base64(s)

/usr/lib/python2.6/base64.pyc in _translate(s, altchars)
     34     for k, v in altchars.items():
     35         translation[ord(k)] = v
---> 36     return s.translate(''.join(translation))
     37 
     38 

TypeError: character mapping must return integer, None or unicode

似乎 json 处理以某种方式破坏了 base64 内容。

4

1 回答 1

4

问题出在编码上。json 返回 base64 模块无法处理的 utf-8 编码文本(它需要 ascii)。因此,更正是在将 json 解码字符串传递给 base64 之前对其进行编码('ascii'):

In [8]: base64.urlsafe_b64decode(
            json.loads(
                json.dumps(base64.urlsafe_b64encode(content))
            )
            .encode('ascii')
        ) == content
Out[7]: True
于 2013-05-02T03:17:20.983 回答