2016-05-14 51 views
13

Tôi đang viết một kịch bản để tự động tạo dữ liệu cho bản trình diễn và tôi cần phải tuần tự hóa trong một số dữ liệu JSON. Một phần của dữ liệu này là một hình ảnh, vì vậy tôi mã hóa nó trong base64, nhưng khi tôi cố gắng chạy kịch bản của tôi, tôi nhận được:Tuần tự hóa trong JSON một dữ liệu được mã hóa 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 

Theo như tôi biết, một bất cứ điều gì base64 mã hóa (một hình ảnh PNG, trong trường hợp này) chỉ là một chuỗi, do đó, nó nên đặt ra vấn đề để serializating. Tôi đang thiếu gì?

Trả lời

21

Bạn phải cẩn thận về các kiểu dữ liệu.

Nếu bạn đọc hình ảnh nhị phân, bạn nhận được byte. Nếu bạn mã hóa các byte này trong base64, bạn sẽ nhận được ... byte một lần nữa! (xem tài liệu trên b64encode)

json không thể xử lý byte thô, đó là lý do bạn gặp lỗi.

Tôi vừa viết một số ví dụ, với ý kiến, tôi hy vọng nó sẽ giúp:

from base64 import b64encode 
from json import dumps 

ENCODING = 'utf-8' 
IMAGE_NAME = 'spam.jpg' 
JSON_NAME = 'output.json' 

# first: reading the binary stuff 
# note the 'rb' flag 
# result: bytes 
with open(IMAGE_NAME, 'rb') as open_file: 
    byte_content = open_file.read() 

# second: base64 encode read data 
# result: bytes (again) 
base64_bytes = b64encode(byte_content) 

# third: decode these bytes to text 
# result: string (in utf-8) 
base64_string = base64_bytes.decode(ENCODING) 

# optional: doing stuff with the data 
# result here: some dict 
raw_data = {IMAGE_NAME: base64_string} 

# now: encoding the data to json 
# result: string 
json_data = dumps(raw_data, indent=2) 

# finally: writing the json string to disk 
# note the 'w' flag, no 'b' needed as we deal with text here 
with open(JSON_NAME, 'w') as another_open_file: 
    another_open_file.write(json_data) 
+0

Tôi đã có một vấn đề tương tự khi tôi còn sử dụng Gmail API để gửi email với hành động cụ thể này 'trở lại { 'thô': base64.urlsafe_b64encode (message.as_string())} '. @spky Cảm ơn câu trả lời của bạn! – InamTaj

+1

Tôi đang làm tương tự cho tệp excel, mọi thứ đang diễn ra chính xác nhưng tệp được ghi vào đĩa bị hỏng, không thể mở được bình thường –

Các vấn đề liên quan