Simple question. Is there a way to read a file in the form of bits (and not bytes or text)? If not, is there a way to convert the bytes that I get from the Python IO (binary mode) into bits?
To add a little context, I am not just trying to read the bits, but also modify them in specific cases and write the modified ones to a new file. I can work with the bits being either in the form of a string or an integer.
EDIT: The data in the original file will be in the form of simple text. What I am trying to do is get the bits that make up each character and modify them to suit my needs.
1 Answer 1
There is a python package "bitstring". The manual is here: https://pythonhosted.org/bitstring/ There is a link on that web page to download the package. I have used it and it can read bits from a file.
bitarraymodule is what you want. But depending on what exactly you want to do, you can also just turn a represent a byte in binary by doing something likebin(ord('A')). See this answer on how to iterate over a file byte by byte.