Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3: Standard way to surpress the b' prefix when printing [duplicate]

I recently started to use Python 3 and I understood that in this version, the print() function assumes that the string is unicode and not ASCII (8-bit chars).

Well, I know that my strings are sometimes ASCII, but I really don't care about it. Why should I always get an annoying b' prefix printed to the screen?

Is there a way to turn off this "feature" ?

My code:

proc = subprocess.Popen(diff_cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
while proc.poll() is None:
    output = proc.stdout.readlines()
    for line in output:
        print(line)
like image 697
SomethingSomething Avatar asked Oct 25 '25 11:10

SomethingSomething


1 Answers

You need to be explicit about this yourself.

You need to specify the encoding and decode the bytes into Unicode (ASCII encoding): line.decode('ascii'). If object are sometimes bytes and sometimes Unicode then you need to handle this accordingly.

After that you can pass it to print().

like image 89
Simeon Visser Avatar answered Oct 28 '25 03:10

Simeon Visser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!