Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

A Python Module for Reading and Writing XMLTV Files

Project description


python-xmltv is a Python module that provides access to XMLTV data. XMLTV is
an XML format for storing TV listings.

More information on XMLTV can be found at

There are no requirements for Python 2.5 and up. For older versions,
ElementTree is required, which can be found at

Usage of the module is generally straight-forward::

import xmltv
from pprint import pprint

# If you need to change the locale:
# xmltv.locale = 'Latin-1'

# If you need to change the date format used in the XMLTV file:
# xmltv.date_format = '%Y%m%d%H%M%S %Z'

filename = '/path/to/xmltv/file'

# Print info for XMLTV file (source, etc.)
pprint(xmltv.read_data(open(filename, 'r')))

# Print channels
pprint(xmltv.read_channels(open(filename, 'r')))

# Print programmes
pprint(xmltv.read_programmes(open(filename, 'r')))

There are currently three functions for reading that should be used:

**read_data** ``(file_object) -> dict``
Get the source and other info from an XMLTV file.

Returns a dictionary of <tv> attributes, eg::

{'date': u'20030702230041 -0300',
'generator-info-name': u'tv_grab_na V3.20030629',
'generator-info-url': u'',
'source-info-name': u'Zap2It',
'source-info-url': u''}

**read_channels** ``(file_object) -> list``
Get all of the channels.

Returns a list of hashes, each representing a channel, eg::

[{'display-name': [(u'Channel 10 ELTV', u'')],
'id': u''},
{'display-name': [(u'Channel 11 CBHT', u'')],
'icon': [{'desc': '','src': u''}],
'id': u''}]

**read_programmes** ``(file_object) -> list``
Get all of the programmes.

Returns a list of hashes, each representing a programme, eg::

[{'audio': [{'stereo': [u'stereo']}],
'category': [(u'Biz', u''), (u'Fin', u'')],
'channel': u'',
'start': u'20030702000000 ADT',
'stop': u'20030702003000 ADT',
'title': [(u'This Week in Business', u'')]},
{'audio': [{'stereo': [u'stereo']}],
'channel': u'',
'desc': [(u'In an effort to grow up, George proposes marriage to former girlfriend Susan.',
'rating': [{'system': u'VCHIP', 'value': u'PG'}],
'start': u'20030702000000 ADT',
'stop': u'20030702003000 ADT',
'sub-title': [(u'The Engagement', u'')],
'subtitles': [{'type': u'teletext'}],
'title': [(u'Seinfeld', u'')]}]

There is also a Writer class. It should always write proper XMLTV data. All
strings, except for dictionary keys, should be in Unicode.

It contains the following methods:

**__init__** ``(fp, encoding="iso-8859-1", date=None, source_info_url=None, source_info_name=None, generator_info_url=None, generator_info_name=None)`` -> ``Writer``
Returns a Writer object.


A File object to write XMLTV data to

The text encoding that will be used. *Defaults to

The date this data was generated. *Optional*

A URL for information about the source of the data. *Optional*

A human readable description of ``source_info_url``.

A URL for information about the program that is generating the
XMLTV document. *Optional*

A human readable description of ``generator_info_url``.

**write_channel** ``(channel)``
Write a channel dictionary

Here's an example channel dictionary::

{'display-name': [(u'Channel 11 CBHT', u'en')],
'icon': [{'src': u''}],
'id': u'',
'url': u:''}

**write_programme** ``(programme)``
Write a programme dictionary

Here's an example programme dictionary::

{'audio': [{'stereo': u'stereo'}],
'category': [(u'Comedy', u'')],
'channel': u'',
'country': [(u'USA', u'')],
'credits': [{'producer': [u'Larry David'], 'actor': [u'Jerry Seinfeld']}],
'date': [u'1995'],
'desc': [(u'In an effort to grow up, George proposes marriage to former girlfriend Susan.',
'episode-num': [(u'7 . 1 . 1/1', u'xmltv_ns')],
'language': [(u'English', u'')],
'last-chance': [(u'Hah!', u'')],
'length': [{'units': u'minutes', 'length': 22}],
'new': [1],
'orig-language': [(u'English', u'')],
'premiere': [(u'Not really. Just testing', u'en')],
'previously-shown': [{'channel': u'',
'start': u'19950921103000 ADT'}],
'rating': [{'icon': [{'height': u'64',
'src': u'http://some.ratings/PGicon.png',
'width': u'64'}],
'system': u'VCHIP',
'value': u'PG'}],
'star-rating': [{'icon': [{'height': u'32',
'src': u'',
'width': u'32'}],
'value': u'4/5'}],
'start': u'20030702000000 ADT',
'stop': u'20030702003000 ADT',
'sub-title': [(u'The Engagement', u'')],
'subtitles': [{'type': u'teletext', 'language': (u'English', u'')}],
'title': [(u'Seinfeld', u'')],
'video': [{'colour': 1, 'aspect': u'4:3', 'present': 1}]}

**end** ``()``
Write end tag

Call this before closing a file.

Reporting Bugs
Please send all bugs, comments, and questions to James Oakley


Version 1.4.3
* Add pretty_print option to Writer.write()

Version 1.4.2
* Text encoding fixes. (Patch from Jan Sušnik)

Version 1.4.1
* Updated to include all doc files

Version 1.4
* Updated to reflect current XMLTV DTD (patch from Igor Sobolev)
* Clean up for pypi release

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-xmltv, version 1.4.3
Filename, size File type Python version Upload date Hashes
Filename, size python-xmltv-1.4.3.tar.gz (21.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page