Last active
August 29, 2015 14:28
-
-
Save mjpieters/43f610d1b69f5bc6db73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> import re | |
| >>> invalid_escape = re.compile(r'\\([1-3][0-7]{2}|[1-7][0-7]?)') # octal digits from 1 up to FF | |
| >>> def replace_with_codepoint(match): | |
| ... return chr(int(match.group(0)[1:], 8)) | |
| ... | |
| >>> def repair(brokenjson): | |
| ... return invalid_escape.sub(replace_with_codepoint, brokenjson) | |
| ... | |
| >>> sample = '{"destination":"Provence-Alpes-C\\303\\264te d\'Azur"}' | |
| >>> repair(sample) | |
| '{"destination":"Provence-Alpes-C\xc3\xb4te d\'Azur"}' | |
| >>> json.loads(repair(sample)) | |
| {u'destination': u"Provence-Alpes-C\xf4te d'Azur"} | |
| >>> print json.loads(repair(sample))['destination'] | |
| Provence-Alpes-Côte d'Azur |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment