Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

write bytes64 in json python

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)
Comment

PREVIOUS NEXT
Code Example
Javascript :: button size react native 
Javascript :: fs.writefilesync in nodejs 
Javascript :: scroll to section react 
Javascript :: jquery select clear options 
Javascript :: js shortcut 
Javascript :: json parse string 
Javascript :: classlist.toggle 
Javascript :: inline if else javascript 
Javascript :: regex any character 
Javascript :: javascript remove characters from beginning of string 
Javascript :: fluttter http get 
Javascript :: find lowest number in array js 
Javascript :: drop down listing in angular form 
Javascript :: string split javascript 
Javascript :: how do i listen to a keypress in javascript 
Javascript :: add sass autoprefixer to react 
Javascript :: jquery value of input 
Javascript :: javascript insert before 
Javascript :: how to check if div is display none jquery 
Javascript :: jquery summernote set value 
Javascript :: javascript e.key 
Javascript :: start angular app server 
Javascript :: jquery detect change in textarea content 
Javascript :: js insert item into array 
Javascript :: convert string to date using moment 
Javascript :: Confirm the Ending 
Javascript :: how to check if website is down javascript 
Javascript :: mongoose find one and update with new field 
Javascript :: javascript try catch 
Javascript :: discord js on message 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =