Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Turn a file content into a binary array

File content:

40 13 123
89 123 2223
4  12  0

I need to store the whole .txt file as a binary array so that I can send it later to the server side which expects a binary input.


I've looked at Python's bytearray documentation. I quote:

Return a new array of bytes. The bytearray type is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the bytes type has, see Bytes and Byte Array Methods.


My numbers are greater than 256, I need a bytearray data structure for numbers that are greater than 256.

like image 773
Tony Tannous Avatar asked Mar 10 '17 09:03

Tony Tannous


People also ask

How do you convert a text file to binary in Python?

Until the end of the text file is reached perform the following steps: Read a line from the input text file, into 3 variables using fscanf(). The structure variable is set to values for elements(to write the structure variable into the output file. Write that structure variable to the output binary file using fwrite().

How do I get the binary content of a file in Python?

Python Read Binary File into Byte Array First, the file is opened in the“ rb “ mode. A byte array called mybytearray is initialized using the bytearray() method. Then the file is read one byte at a time using f. read(1) and appended to the byte array using += operator.

What is binary mode in python?

Practical Data Science using Python "Binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like JPEGs or GIFs, audio files like MP3s or binary document formats like Word or PDF. In Python, files are opened in text mode by default.

How do you write binary numbers in Python?

In Python, you can simply use the bin() function to convert from a decimal value to its corresponding binary value. And similarly, the int() function to convert a binary to its decimal value. The int() function takes as second argument the base of the number to be converted, which is 2 in case of binary numbers.


2 Answers

you might use the array/memoryview approach

import array
a = array.array('h', [10, 20, 300]) #assume that the input are short signed integers
memv = memoryview(a)
m = memv.cast('b') #cast to bytes
m.tolist()

this then gives [10, 0, 20, 0, 44, 1]

Depending on the usage, one might also do:

L = array.array('h', [10, 20, 300]).tostring()
list(map(ord, list(L)))

this also gives [10, 0, 20, 0, 44, 1]

like image 111
ewcz Avatar answered Sep 29 '22 10:09

ewcz


You can read in the text file and convert each 'word' to an int:

with open(the_file, 'r') as f:
    lines = f.read_lines()
    numbers = [int(w) for line in lines for w in line.split()]

Then you have to pack numbers into a binary array with struct:

binary_representation = struct.pack("{}i".format(len(numbers)), *numbers)

If you want these data to be written in binary format, you have to specify so when opening the target file:

with open(target_file, 'wb') as f:
   f.write(binary_representation)
like image 27
xtofl Avatar answered Sep 29 '22 09:09

xtofl