Correct way to octal escape a UTF-8 character in Python -


I need to get the octave escape sequence for UTF-8 characters in Python and wondering what to do Something I want to do, like something in the standard library that I ignored is a temporary string manipulating function but I hope that is a better solution

I want to get (like) I:

From: \ 360 \ 220 \ 205 \ 245

Right now I am doing this:

  char = '\ U00010165' # This way Python keeps my hands above it char = str (char .encode ()) # char = "b" \ xf0 \ xft ("\\ x" in I arr: four + = '\\' + str (act (intestine (i, 16)) # char = \ 0o360 \ 0o220 \ 0o205 \ 0o245 char = char.replace ("0o", "")  

any suggestions?

Use the format (i, '03o') to format octal numbers without any written 0o indicator For, or to include str.format () literal backslash For areas:

  & gt; & Gt; & Gt; Format (16, '03 o ')' 020 '& gt; & Gt; & Gt; '\\ {: 03o}'. Format (16) '\\ 020'  

and just loop on encoded byte value; Each character is given as an integer:

  char = '' .join (['\\ {: 03o}'. C.encode for format (c) Utf8 ')]]  

demo:

  & gt; & Gt; & Gt; Char = '\ U00010165' & gt; & Gt; & Gt; Format 'c' \ '360 \ Gt; & gt; print (''. ('' \\ {: 03o} '' in the format for Ccode. (C) ('utf8')]) \ 360 \ 220 \ 205 \ 245  

Comments