13
我正在编写一个脚本来为演示自动生成数据,我需要在JSON中序列化一些数据。该数据的一部分是图像,所以我在base64编码的,但是当我尝试运行我的脚本,我得到:用JSON序列化base64编码数据
Traceback (most recent call last):
File "lazyAutomationScript.py", line 113, in <module>
json.dump(out_dict, outfile)
File "/usr/lib/python3.4/json/__init__.py", line 178, in dump
for chunk in iterable:
File "/usr/lib/python3.4/json/encoder.py", line 422, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/usr/lib/python3.4/json/encoder.py", line 396, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.4/json/encoder.py", line 396, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.4/json/encoder.py", line 429, in _iterencode
o = _default(o)
File "/usr/lib/python3.4/json/encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'iVBORw0KGgoAAAANSUhEUgAADWcAABRACAYAAABf7ZytAAAABGdB...
...
BF2jhLaJNmRwAAAAAElFTkSuQmCC' is not JSON serializable
据我所知,一个base64编码,无论(PNG图像,在这种情况下)只是一个字符串,所以它应该对序列化造成问题。我错过了什么?
我有一个类似的问题,当我使用Gmail API发送电子邮件与此特定行动'返回{“原始”: base64.urlsafe_b64encode(message.as_string())}'。 @spky感谢您的回答! – InamTaj
我对Excel文件做的是一样的,一切正常,但写入磁盘的文件已损坏,无法正常打开 –