binary-data

Searching for sequence of bits in an integer in Python

Searching for sequence of bits in an integer in Python Question: I have two integers, lets call them haystack and needle. I need to check that, if the binary representation of needle occurred in haystack [and OPTIONALLY find the position of the first occurrence] Example haystack = 0b10101111010110010101010101110 needle = 0b1011001 # occurred in position …

Total answers: 2

Binary numbers returns all 0

Binary numbers returns all 0 Question: I’m trying to convert my web scraping data into binary numbers. Basically, if the class name contains yes it is equal to 1 and no is equal to 0. When I print out the binary_value, it returns all 0 even though it contains yes. I’m not really sure what …

Total answers: 1

[PYTHON/BINARY FILE]: Sorting the bits read

[PYTHON/BINARY FILE]: Sorting the bits read Question: The file ‘binary_file.bin’ contains the following binary data (hexadecimal base used for simplicity): A43CB90F Each 2 bytes correspond to an unsigned integer of 16 bits: first number is A43C and second number is B90F, which in decimal base correspond respectively to 42044 and to 47375. I’m trying to …

Total answers: 1

Error! blahfile is not UTF-8 encoded. Saving disabled

Error! blahfile is not UTF-8 encoded. Saving disabled Question: So, I’m trying to write a gzip file, actually from the net, but to simplify I wrote some very basic test. import gzip LINES = [b’I am a test line’ for _ in range(100_000)] f = gzip.open(‘./test.text.gz’, ‘wb’) for line in LINES: f.write(line) f.close() It runs …

Total answers: 2

How do you set a string of bytes from an environment variable in Python?

How do you set a string of bytes from an environment variable in Python? Question: Say that you have a string of bytes generated via os.urandom(24), b’x1bxbax94(xaexd0xb2xa6xf2fxf6x1fIxedxbao$xc6Dx08xbax81x96v’ and you’d like to store that in an environment variable, export FOO=’x1bxbax94(xaexd0xb2xa6xf2fxf6x1fIxedxbao$xc6Dx08xbax81x96v’ and retrieve the value from within a Python program using os.environ. foo = os.environ[‘FOO’] The problem …

Total answers: 4

convert binary string to numpy array

convert binary string to numpy array Question: Assume I have the string: my_data = ‘x00x00x80?x00x00x00@x00x00@@x00x00x80@’ Where I got it is irrelevant, but for the sake of having something concrete, assume I read it from a binary file. I know my string is the binary representation of 4 (4-byte) floats. I would like to get those …

Total answers: 2

best way to preserve numpy arrays on disk

best way to preserve numpy arrays on disk Question: I am looking for a fast way to preserve large numpy arrays. I want to save them to the disk in a binary format, then read them back into memory relatively fastly. cPickle is not fast enough, unfortunately. I found numpy.savez and numpy.load. But the weird …

Total answers: 7

Use binary COPY table FROM with psycopg2

Use binary COPY table FROM with psycopg2 Question: I have tens of millions of rows to transfer from multidimensional array files into a PostgreSQL database. My tools are Python and psycopg2. The most efficient way to bulk instert data is using copy_from. However, my data are mostly 32-bit floating point numbers (real or float4), so …

Total answers: 2