Kylian Kylian -4 years ago 155
Python Question

Binary with Python letter b delete or what its meaning?

I just started with Python and want to find the binary code for any given character in a text file. The problem I encountered is when it prints the binary there is a "b" in the binary.

file = open("textfile.txt","w")
file.write("Hello World ")
file.write("This our new text file")
file.write("and this is another line. ")
file.write("Why? Because we can.")
file.close()
with open("textfile.txt") as file:
data=file.readline()
data_vector = list(data)
binary_data_vector = map(bin, bytearray(data_vector))
print(binary_data_vector)


This is the output I am currently getting:

['0b1001000', '0b1100101', '0b1101100', '0b1101100', '0b1101111', '0b100000', '0b1010111', '0b1101111', '0b1110010', '0b1101100', '0b1100100', '0b100000', '0b1010100', '0b1101000', '0b1101001', '0b1110011', '0b100000', '0b1101111', '0b1110101', '0b1110010', '0b100000', '0b1101110', '0b1100101', '0b1110111', '0b100000', '0b1110100', '0b1100101', '0b1111000', '0b1110100', '0b100000', '0b1100110', '0b1101001', '0b1101100', '0b1100101', '0b1100001', '0b1101110', '0b1100100', '0b100000', '0b1110100', '0b1101000', '0b1101001', '0b1110011', '0b100000', '0b1101001', '0b1110011', '0b100000', '0b1100001', '0b1101110', '0b1101111', '0b1110100', '0b1101000', '0b1100101', '0b1110010', '0b100000', '0b1101100', '0b1101001', '0b1101110', '0b1100101', '0b101110', '0b100000', '0b1010111', '0b1101000', '0b1111001', '0b111111', '0b100000', '0b1000010', '0b1100101', '0b1100011', '0b1100001', '0b1110101', '0b1110011', '0b1100101', '0b100000', '0b1110111', '0b1100101', '0b100000', '0b1100011', '0b1100001', '0b1101110', '0b101110']


So my question is how can I get rid of "b" so it prints only 8 bits for each character. And if you know why this happens, please explain!.

Answer Source

The bin function returns the binary representation of a number, prefixed with 0b which makes such a representation suitable directly for input in Python source code.

It is not what we want most of the time, so one valid thing would be to strip the first 2 chars of each string. But then there is another issue with bin: it only encodes as many bits of a number as needed to represent it. That means that ASCII characters would be encoded in 7 binary digits, when we usually want 8 bits on the representation.

The alternative is to use string formatting itself for creating the representation. The .format string method allows you not only to ask for the binary (rather than decimal) representation of a number be printed, but further allows you to specify how many digits you want by typing in the leading zeros in the format string:

binary_data_vector = ["{:08b}".format(number) for number in bytearray(data_vector)]

(Take time to look at list comprehensions - they look strange at first, but are far more powerful and readable than using the map and filter functions - the above line, for example, would require specifying a lambda function to be written using map as you had it)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download