print in shell: b ??? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: print in shell: b ??? (/thread-42548.html) Pages:
1
2
|
print in shell: b ??? - trix - Aug-06-2024 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. RE: print in shell: b ??? - deanhystad - Aug-06-2024 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. RE: print in shell: b ??? - Gribouillis - Aug-06-2024 (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' >>> RE: print in shell: b ??? - trix - Aug-06-2024 o sorry I forgot to attach a picture RE: print in shell: b ??? - Gribouillis - Aug-06-2024 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' RE: print in shell: b ??? - trix - Aug-07-2024 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 ? RE: print in shell: b ??? - deanhystad - Aug-07-2024 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. RE: print in shell: b ??? - trix - Aug-07-2024 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. RE: print in shell: b ??? - deanhystad - Aug-08-2024 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. Common escape sequences are used too. For other characters the value is displayed as a hexidecimal number. 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) 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) 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) 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)
RE: print in shell: b ??? - trix - Aug-08-2024 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. |