0

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.

martineau
124k29 gold badges181 silver badges319 bronze badges
asked Feb 26, 2015 at 19:36
12
  • 2
    Can't you read them as bytes, then manipulate them with bitwise operations? Commented Feb 26, 2015 at 19:39
  • 1
    Read in bytes, convert bytes into bits, manipulate bits, convert bits back to bytes, write to file. Commented Feb 26, 2015 at 20:01
  • 1
    That is what I am trying to do. What I am trying to figure out is how to convert the bytes into bits. Commented Feb 26, 2015 at 20:06
  • 1
    Sounds like the bitarray module 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 like bin(ord('A')). See this answer on how to iterate over a file byte by byte. Commented Feb 26, 2015 at 20:10
  • 1

1 Answer 1

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.

answered Feb 26, 2015 at 23:36
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.