Python Forum
print is showing non printable characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print is showing non printable characters
#2
turtleman Wrote:What am I doing wrong?
You're not doing anything wrong. It is the way python prints bytes objects
>>> x = b'foo bar'
>>> print(x)
b'foo bar'
You can use the decode() method, without argument if the bytes string is encoded in utf8
>>> print(x.decode())
foo bar
Starting with python 3.7, an alternative is to use text=True in the call to subprocess.run(). Before that, you can use universal_newlines=True. Then the output will be a str instance instead of a bytes. Starting with python 3.6, you can use encoding='utf8' or another encoding value.
output = subprocess.run(['dir'], stdout=subprocess.PIPE, text=True)
also you don't need shell=True. Use a list as first argument of run().
Reply


Messages In This Thread
RE: print is showing non printable characters - by Gribouillis - May-04-2019, 09:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  email Library: properly send message as quoted-printable malonn 3 1,376 Nov-14-2022, 09:31 PM
Last Post: malonn
  is this Unicode printable? Skaperen 2 1,470 Sep-23-2021, 01:25 AM
Last Post: Skaperen
  Print characters in a single line rather than one at a time hhydration 1 2,060 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  get two characters, count and print from a .txt file Pleiades 9 3,440 Oct-05-2020, 09:22 AM
Last Post: perfringo
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,914 May-15-2020, 01:37 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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