Skip to main content

A li'l class for data URI manipulation in Python

Project description

Data URI manipulation made easy.

This isn’t very robust, and will reject a number of valid data URIs. However, it meets the most useful case: a mimetype, a charset, and the base64 flag.


$ pip install python-datauri


>>> from datauri import DataURI
>>> uri = DataURI('data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu')
>>> uri.mimetype
>>> uri.charset
>>> uri.is_base64
b'The quick brown fox jumped over the lazy dog.'

Note that will always return bytes, (which in Python 2 is the same as a string). Use DataURI.text to get the text type (str on Python 3.x, unicode on Python 2.x).

Creating from a string

>>> from datauri import DataURI
>>> made = DataURI.make('text/plain', charset='us-ascii', base64=True, data='This is a message.')
>>> made
b'This is a message.'

Creating from a file

This is really just a convenience method.

>>> from datauri import DataURI
>>> png_uri = DataURI.from_file('somefile.png')
>>> png_uri.mimetype


This code is released under the Unlicense.


This is a repackaging of this Gist originally written by Zachary Voase.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for python-datauri, version 0.2.9
Filename, size File type Python version Upload date Hashes
Filename, size python_datauri-0.2.9-py2.py3-none-any.whl (5.8 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size python-datauri-0.2.9.tar.gz (6.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page