Extract time from datetime and determine if time (not date) falls within range?

This line: str_time = datetime.strptime(Datetime, “%m/%j/%y %H:%M”) returns a datetime object as per the docs. You can test this yourself by running the following command interactively in the interpreter: >>> import datetime >>> datetime.datetime.strptime(’12/31/13 00:12′, “%m/%j/%y %H:%M”) datetime.datetime(2013, 1, 31, 0, 12) >>> The time portion of the returned datetime can then be accessed using … Read more

Python 2.6 JSON decoding performance

The new Yajl – Yet Another JSON Library is very fast. yajl serialize: 0.180 deserialize: 0.182 total: 0.362 simplejson serialize: 0.840 deserialize: 0.490 total: 1.331 stdlib json serialize: 2.812 deserialize: 8.725 total: 11.537 You can compare the libraries yourself. Update: UltraJSON is even faster.

Remove whitespaces in XML string

The easiest solution is probably using lxml, where you can set a parser option to ignore white space between elements: >>> from lxml import etree >>> parser = etree.XMLParser(remove_blank_text=True) >>> xml_str=””‘<root> >>> <head></head> >>> <content></content> >>> </root>”’ >>> elem = etree.XML(xml_str, parser=parser) >>> print etree.tostring(elem) <root><head/><content/></root> This will probably be enough for your needs, but … Read more

Any gotchas using unicode_literals in Python 2.6?

The main source of problems I’ve had working with unicode strings is when you mix utf-8 encoded strings with unicode ones. For example, consider the following scripts. two.py # encoding: utf-8 name=”helló wörld from two” one.py # encoding: utf-8 from __future__ import unicode_literals import two name=”helló wörld from one” print name + two.name The output … Read more