我怎样才能让python base64编码与json一起玩得很好?

时间:2013-05-02 03:17:21

标签: python json base64

我有大型二进制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内容。

1 个答案:

答案 0 :(得分: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