Splitting strings in Python while not ignoring escape characters

Splitting strings in Python is easy:

"this is a string".split() # default delimiter is a space (' ')
# ['this', 'is', 'a', 'string']

However, when your faced with escape characters, things become a tad more difficult:

"this is\ a string".split()
# ['this', 'is\\', 'a', 'string']

Of course you could write your own split function that takes care of these escape characters, but there’s an easier way still:

import cStringIO as c
import csv
csv.reader(c.StringIO("this is\ a string"), delimiter=' ', escapechar='\\').next()
# ['this', 'is a', 'string']