-1
x
Out[18]: '\x1b[24m\x1b[1m\x1b[2;1HPressure Index      \r\n'

print x
Pressure Index      

str(x)
Out[20]: '\x1b[24m\x1b[1m\x1b[2;1HPressure Index      \r\n'

x.__str__()
Out[21]: '\x1b[24m\x1b[1m\x1b[2;1HPressure Index      \r\n'

I don't understand how the print statement cleans up all the garbage characters in the variable. I already have a work around, by splitting the text after "1H" and stripping the new line characters, but I am curious how I could do this more rigorously and get the clean output of print to become my variable. What is it that the print statement is doing to clean up the text?

After doing some further reading, I tried to implement __str__ as part of a class, but I am having trouble implementing this. Below is my first attempt

 class readable(str):
    def __str__(self):
        return str(self)

But I still get

y=readable(x)

y
Out[50]: '\x1b[24m\x1b[1m\x1b[2;1HPressure Index      \r\n'

EDIT: The output that I want is the print statement output. I am trying to strip my variable of all the extra characters so that it will just be a string reading 'Pressure Index'

Novice
  • 855
  • 8
  • 17

1 Answers1

2

\x1b is the Escape character (which can also be written as \e in a string), and ESC [ <number> m is the escape sequence for changing various text modes (bold, underline, color, etc.). print prints control characters as they are, rather than trying to display them as escape sequences, so you don't see them in the output, they have their desired effect on the terminal. You can read about these escape sequences in the Wikipedia page on ANSI Escape Sequences.

If you want to set a variable to the representation that shows with escape sequences, use the repr() function:

y = repr(x)
print y
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • When I use `repr` i get the following output `x Out[51]: '\x1b[24m\x1b[1m\x1b[2;1HPressure Index \r\n' repr(x) Out[52]: "'\\x1b[24m\\x1b[1m\\x1b[2;1HPressure Index \\r\\n'"` What I want is the clean output that print uses. The one with all of the escape characters and such stripped – Novice Mar 27 '18 at 17:38
  • @Novice: But `print` does *not* strip these characters! It sends them to the terminal, and this, in turn, *also* does not strip them but *uses* them. You can replace a `\t` with spaces (but with how many?) but with what would you replace `\n` to indicate an end-of-line? It smells of an X-Y problem: what do you hope to solve by doing this? – Jongware Mar 27 '18 at 17:45
  • I am reading from a serial port and displaying the output in a GUI message box, so I don't want to confuse people with all the extra characters. I need to pass what is read from the serial port to my GUI thread and then display it, which won't use print. I already have a workaround that works 95% of the time, but was wondering if I could mimic what was being done by the print statement to get it to work 100% of the time. I don't really need any of the formatting characters since I handle new lines in my GUI application. Each new message gets a new line – Novice Mar 27 '18 at 17:52
  • It sounds like you need a library that translates or removes ANSI escape codes when displaying the string in the message box. Take a look [here](https://stackoverflow.com/questions/12492810/python-how-can-i-make-the-ansi-escape-codes-to-work-also-in-windows) as a starting point. – Barmar Mar 27 '18 at 18:05
  • Thanks. I get the downvote since I worded my question poorly. You told me what the print statement is doing, so you answered my initial question – Novice Mar 27 '18 at 18:09
  • Oh, I didn't assume you did, just a general statement. Thank you so much. Really helped me out – Novice Mar 27 '18 at 18:14