implement 'parse_unicode_escapes()'

This commit is contained in:
Mike Fährmann
2019-06-16 21:46:26 +02:00
parent 3a36a0fa1e
commit b171befa87
4 changed files with 26 additions and 1 deletions

View File

@@ -174,6 +174,17 @@ def extract_from(txt, pos=0, default=""):
return extr
def parse_unicode_escapes(txt):
"""Convert JSON Unicode escapes in 'txt' into actual characters"""
if "\\u" in txt:
return re.sub(r"\\u([0-9a-fA-F]{4})", _hex_to_char, txt)
return txt
def _hex_to_char(match):
return chr(int(match.group(1), 16))
def parse_bytes(value, default=0, suffixes="bkmgtp"):
"""Convert a bytes-amount ("500k", "2.5M", ...) to int"""
try: