Getting correct string length in Python for strings with ANSI color codes

The pyparsing wiki includes this helpful expression for matching on ANSI escape sequences: ESC = Literal(‘\x1b’) integer = Word(nums) escapeSeq = Combine(ESC + ‘[‘ + Optional(delimitedList(integer,’;’)) + oneOf(list(alphas))) Here’s how to make this into an escape-sequence-stripper: from pyparsing import * ESC = Literal(‘\x1b’) integer = Word(nums) escapeSeq = Combine(ESC + ‘[‘ + Optional(delimitedList(integer,’;’)) + oneOf(list(alphas))) … Read more

How to get Uri.EscapeDataString to comply with RFC 3986

Having not been able to get Uri.EscapeDataString to take on RFC 3986 behavior, I wrote my own RFC 3986 compliant escaping method. It leverages Uri.EscapeDataString, and then ‘upgrades’ the escaping to RFC 3986 compliance. /// <summary> /// The set of characters that are unreserved in RFC 2396 but are NOT unreserved in RFC 3986. /// … Read more

Python: json.loads chokes on escapes [duplicate]

The correct json is: r'{“FileExists”: true, “Version”: “4.3.2.1”, “Path”: “\\\\host\\dir\\file.exe”}’ Note the letter r if you omit it you need to escape \ for Python too. >>> import json >>> d = json.loads(s) >>> d.keys() [u’FileExists’, u’Path’, u’Version’] >>> d.values() [True, u’\\\\host\\dir\\file.exe’, u’4.3.2.1′] Note the difference: >>> repr(d[u’Path’]) “u’\\\\\\\\host\\\\dir\\\\file.exe'” >>> str(d[u’Path’]) ‘\\\\host\\dir\\file.exe’ >>> print d[u’Path’] … Read more

escaping inside html tag attribute value

There are two types of “escapes” involved here, HTML and JavaScript. When interpreting an HTML document, the HTML escapes are parsed first. As far as HTML is considered, the rules within an attribute value are the same as elsewhere plus one additional rule: The less-than character < should be escaped. Usually &lt; is used for … Read more