Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

json to base64 python

>>> import json
>>> import base64
>>> d = {"alg": "ES256"} 
>>> s = json.dumps(d)  # Turns your json dict into a str
>>> print(s)
{"alg": "ES256"}
>>> type(s)
<class 'str'>
>>> base64.b64encode(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.2/base64.py", line 56, in b64encode
    raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not str
>>> base64.b64encode(s.encode('utf-8'))
b'eyJhbGciOiAiRVMyNTYifQ=='
Comment

json to base64 python

data = '{"hello": "world"}'
enc = data.encode()  # utf-8 by default
print base64.encodestring(enc)
Comment

PREVIOUS NEXT
Code Example
Python :: reverse key order dict python 
Python :: check input in python 
Python :: create new dataframe from existing dataframe pandas 
Python :: merge two Python dictionaries in a single expression 
Python :: pandas Unnamed: 0 
Python :: matplotlib cheatsheet 
Python :: numpy sort array by another array 
Python :: python check if all caps 
Python :: calculate angle between 3 points python 
Python :: import local module python 
Python :: python pillow resize image 
Python :: how to resize windows in python 
Python :: how to find the datatype of a dataframe in python 
Python :: create dict from two columns pandas 
Python :: how to round off values in columns in pandas in excel 
Python :: # invert a dictionary 
Python :: pyspark left join 
Python :: how to import file from another directory in python 
Python :: dataframe to list pyspark 
Python :: assign multiple variables in python 
Python :: python glob 
Python :: how to export DataFrame to CSV file 
Python :: robust scaler 
Python :: matplotlib overlapping labels 
Python :: how do i convert a list to a string in python 
Python :: Iterating With for Loops in Python Using range() and len() 
Python :: Current date and time or Python Datetime today 
Python :: django fixtures. To dump data 
Python :: select multiple columns in pandas dataframe 
Python :: python write binary 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =