Serialize in JSON a base64 encoded data

后端 未结 3 1629
别跟我提以往
别跟我提以往 2020-12-07 13:57

I\'m writing a script to automate data generation for a demo and I need to serialize in a JSON some data. Part of this data is an image, so I encoded it in base64, but when

3条回答
  •  难免孤独
    2020-12-07 14:39

    You must be careful about the datatypes.

    If you read a binary image, you get bytes. If you encode these bytes in base64, you get ... bytes again! (see documentation on b64encode)

    json can't handle raw bytes, that's why you get the error.

    I have just written some example, with comments, I hope it helps:

    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)
    

提交回复
热议问题