Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
when repr() fails
#1
what function can convert a string containing unprintable binary characters to source code compatible escape sequences that can restore that binary character when that escape sequence gets parsed as part of a source code string literal?

i thought it to be repr() but it isn't since it does not convert some character values to the escape sequence.


Output:
Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> ord('\377') 255 >>> chr(255) '\xff' >>> repr(chr(255)) "'\xff'" >>> print(repr(chr(255))) Traceback (most recent call last):   File "<stdin>", line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode character '\xff' in position 1: ordinal not in range(128) >>> a=repr(chr(255)) >>> a "'\xff'" >>> a[0] "'" >>> a[1] '\xff' >>> a[2] "'" >>> repr(chr(255))[1] '\xff' >>>
in the above, it can be seen that the character with the binary value of 255 gets "converted" to a single character with the value of 255.  it is the python interactive tool showing us the \xff to represent that value.  i want a function that converts that character (value 255) to \xff which works in source code:

Output:
Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> len('\xff') 1 >>> ord('\xff') 255 >>>
this shows how a script can code eiither \xff or \377 to get a single character with the value 255.  a function that can convert to either hexadecimal form or octal form is suitable.


Output:
lt1/forums /home/forums 13> cat ff.py c = '\xff' print(len(c)) print(ord(c)) c = '\377' print(len(c)) print(ord(c)) lt1/forums /home/forums 14> py3 ff.py 1 255 1 255 lt1/forums /home/forums 15>
then there is reprlib.repr().  but it has exactly the same issues.  before i go code up my own version of repr() can someone point me to the correct function somewhere within python?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
when repr() fails - by Skaperen - May-01-2017, 02:25 AM
RE: when repr() fails - by wavic - May-01-2017, 07:29 AM
RE: when repr() fails - by Skaperen - May-01-2017, 09:34 AM
RE: when repr() fails - by volcano63 - May-01-2017, 07:30 AM
RE: when repr() fails - by Ofnuts - May-01-2017, 03:52 PM
RE: when repr() fails - by snippsat - May-01-2017, 04:37 PM
RE: when repr() fails - by Skaperen - May-02-2017, 03:54 AM
RE: when repr() fails - by snippsat - May-02-2017, 02:32 PM
RE: when repr() fails - by Skaperen - May-03-2017, 02:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Repr() function Valentina 3 3,515 Aug-22-2019, 11:28 AM
Last Post: perfringo
  ascii() (repr() in py2) Skaperen 1 4,001 Jun-05-2017, 10:32 PM
Last Post: Ofnuts
  str vs repr Skaperen 9 6,188 Jun-05-2017, 01:04 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020