Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print in shell: b ???
#1
hello,

i have a simple question, when i print something in the shell there apears a b in front of the variable.
what means the b ?

thank you.
I'm not French, but from the netherlands
Reply
#2
Does the output look something like this: b'\x01\x02\x03\x04\x05' or this b'these are bytes'? The b indicates that you are printing a bytes object.
Reply
#3
(Aug-06-2024, 06:45 PM)trix Wrote: when i print something in the shell there apears a b in front of the variable.
What are you printing? If the object is a bytes string (which is different from a unicode string), youll see a b
>>> s = "hello" # a unicode string
>>> print(s)
hello
>>> t = s.encode() # a bytes string
>>> print(t)
b'hello'
>>> 
« We can solve any problem by introducing an extra level of indirection »
Reply
#4
o sorry Sick
I forgot to attach a picture

[Image: Afbeelding-van-WhatsApp-op-2024-08-06-om...8x9w8&dl=1]
I'm not French, but from the netherlands
Reply
#5
Python writes a b because you are printing bytes strings. That is the normal way to print a bytes string.
>>> print(b'foo')
b'foo'
« We can solve any problem by introducing an extra level of indirection »
Reply
#6
the data that comes in via the UART, the comes from a home-made scanner.
There are 120 IR transistor receivers on it, which check whether they are "light or dark", which then gives a "1" or a "0".
so I receive a "1" or a "0" 120 times. and I will do further calculations with that.
What is the best way to process the incoming data?

for example, is there a way to receive the data byte by byte, and not 1 long series of bytes ?
I'm not French, but from the netherlands
Reply
#7
What is a 1 or a 0? Is it 1 bit, 1 byte, 4 bytes?

Why would you want to recv a byte at a time? bytes is iterable and indexible. You can look at any "byte" (actually an int) you like.
Reply
#8
1 bit
every infrared transistor produces 1 bit
the bytes are now separated by a \ sign.
it would be nice if each byte arrived separately.
I'm not French, but from the netherlands
Reply
#9
The "\" is an artifact of printing the bytes object (just like the b). The data does not contain any "\x". The bytes object is trying to find a way to print each byte. If a byte has the same integer value as a printable ascii character, the character is used in the output.
Output:
>>> x = b"These are all visible" >>> x b'These are all visible'
Common escape sequences are used too.
Output:
>>> x = b"This\thas\rescape\nsequences" >>> x b'This\thas\rescape\nsequences'
For other characters the value is displayed as a hexidecimal number.
Output:
>>> x = bytes((1, 2, 3, 4)) >>> x b'\x01\x02\x03\x04'
And here is a fun mixture:
>>> x = bytes((1, 9, 65, 66, 2)) 
>>> x
b'\x01\tAB\x02'
1 is displayed as the hex number \x01. 9 shows up as a tab (\t). 65 and 66 are the are the decimal values for ascii characters A and B. 2 is displayed as the hex number \x02.

Python is not great about working with bits. To be fair, no languages are great when working with bits. In your example (from the screenshot) I think data will have 15 bytes (15 bytes * 8 bits / byte = 120 bits). You can unpack you bytes into a list of 0 and 1 (not bits, but int objects 0 and 1).
data = bytes((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15))
print("data as bytes", data)
print("data[0]", data[0])
bits = []
for byte in data:
    for x in (128, 64, 32, 16, 8, 4, 2, 1):
        bits.append(int(bool(byte & x)))
print(bits)
Output:
data as bytes b'\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' data[0] 1 [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1]
Notice the first 2 bytes are 00000001 and 00000010, the bits for 1 and 2.

Another way you could do this is convert the bytes to an int, that convert the int to a binary str.
data = bytes((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15))
print("data as bytes", data)
big_int = int.from_bytes(data, "big")
print("Converted to int", big_int, hex(big_int))
bit_str = bin(big_int)
print("As binary str", bit_str)
Output:
data as bytes b'\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' Converted to int 5233100606242806050955395731361295 0x102030405060708090a0b0c0d0e0f As binary str 0b10000001000000011000001000000010100000110000001110000100000001001000010100000101100001100000011010000111000001111
There's a little confusion here because leading zeros are not displayed in hex or binary representation. Since the bit str is just a string, we can pad with leading zeros.
data = bytes((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15))
print("data as bytes", data)
big_int = int.from_bytes(data, "big")
bit_str = f"{big_int:0120b}"
print(bit_str)
Output:
data as bytes b'\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' 000000010000001000000011000001000000010100000110000001110000100000001001000010100000101100001100000011010000111000001111
There is also a function in numpy that will convert an array of bytes to an array of 0 and 1.
import numpy as np

data = np.array((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), np.uint8)
bits = np.unpackbits(data)
print(data)
print(bits)
Output:
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] [0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1]
Gribouillis likes this post
Reply
#10
thanks for the detailed answer, very educational.
I think that last one is what I'm looking for, I'll gonna see how I can apply that.
thanks again.

edit:
why:
9 = t
10 = n
in the ascii table i see something different for these numbers.
I'm not French, but from the netherlands
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print func textbox instead of shell zykbee 1 5,522 Dec-06-2017, 08:22 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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