12 bytes array to double python - python

Im trying to do this:
struct.unpack('d', barray[:HALF_BYTES])[0]
Where barray[:HALF_BYTES] is a 12 bytes array
But I'm getting this error:
Traceback (most recent call last):
File "random_input_sample_drawer.py", line 19, in <module>
print (struct.unpack('d', barray[:HALF_BYTES])[0])
struct.error: unpack requires a bytes object of length 8
How can I solve it ?

You could use:
int.from_bytes(barray[:HALF_BYTES], byteorder='big', signed=False)
to convert those 12 bytes to an integer, if thats what you are trying to do, assuming your barray is a bytearray() and you are using python 3.2 +

Related

Unpack Issue : unpack requires a buffer of 31 bytes

May I know why the following results in as such ?
Code
from struct import *
size = 30
sample = bytes(size)
print(sample)
print(len(sample))
body = unpack("10sHBB4sB8sHB", sample)
print(body[0])
Result:
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
30
Traceback (most recent call last):
File "/home/src/python/sample.py", line 6, in <module>
body = unpack("10sHBB4sB8sHB", sample)
struct.error: unpack requires a buffer of 31 bytes
Python version : 3.9.2
Your format string requires 31 bytes because Python structs use native alignment by default. Therefore, it inserts a padding byte for the second H (unsigned short), because otherwise it would end up at an odd offset (27). To avoid padding, you have to disable alignment by preceding the format string with the = flag character.
>>> from struct import *
>>> calcsize ("10sHBB4sB8sHB")
31
>>> calcsize ("=10sHBB4sB8sHB")
30
This is explained in great detail in the documentation of the struct module.

TypeError: 'numpy.float64' object cannot be interpreted as an integer fake news detection

I am getting this error and not able to resolve it and not able to find it on the internet.
TypeError: 'numpy.float64' object cannot be interpreted as an integer
TypeError Traceback (most recent call last)
<ipython-input-10-33f2a17ec582> in <module>
20 print("Saving New CSV file")
21 if __name__=='__main__':
---> 22 dataSetExtraction()
<ipython-input-10-33f2a17ec582> in dataSetExtraction()
6 dfReal=processRealNewsDataFrame(dfReal)
7 dfCombine=[]
----> 8 for d in extractTopRealResultsForCrawling(dfReal):
9 print('len of datadrame :',d['URL'].size)
10 #d=d[:100]
<ipython-input-6-9dbfd3f21499> in extractTopRealResultsForCrawling(dfReal)
6 listOfIndex=[]
7 df=[]
----> 8 for i in range(0,loop):
9 listOfIndex.append(dfReal[i*10000:(i+1)*10000])
10 df+=[dfReal[i*10000:(i+1)*10000]]
TypeError: 'numpy.float64' object cannot be interpreted as an integer
This is code giving the error. I have not been able to remove the error Please help me
def extractTopRealResultsForCrawling(dfReal):
print("Retrieve top 20000 Real news data")
num=dfReal.size
loop=num/10000
listOfIndex=[]
df=[]
for i in range(0,loop):
listOfIndex.append(dfReal[i*10000:(i+1)*10000])
df+=[dfReal[i*10000:(i+1)*10000]]
#print "length of dataframe array retrieved:",len(df[0])
return df[:LEN]
The range function can only receive integer values
Here is a minimal code reproducing (more or less) the problem:
>>> a = 2.0
>>> [i for i in range(a)]
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
[i for i in range(a)]
TypeError: 'float' object cannot be interpreted as an integer
You need to convert the value to an integer
>>> [i for i in range(int(a))]
[0, 1]
In your code you should use:
for i in range(int(loop)):
Alternatively, you could do:
for i in range(0, num, 10000):
listOfIndex.append(dfReal[i:i+10000])
df+=[dfReal[i:i+10000]]
avoiding the division...

Why TypeError: an integer is required when using cffi?

I'm using cffi and I'm getting a strange error. My function takes a byte array and decrypts it and creates a human-readable string from it.
buffer = [b'\x02', b'+', .... and more]
c_string = ffi.new("char[]", 64)
clib.decode_my_string(buffer, len(buffer), c_string, len(c_string))
print(c_string)
What expect to see is an english string. Python says:
TypeError: an integer is required
Here's how these things are defined:
print(clib.decode_my_string)
print(c_string)
print(buffer)
print(len(c_string))
print(len(buffer))
shows:
<cdata 'uint8_t(*)(uint8_t *, uint8_t, char *, uint16_t)' 0x7f6965aa7a50>
<cdata 'char[]' owning 64 bytes>
[b'\x02', b'+', .... and more]
64
13
From the prints, we can see that the function has two integers involved. An 8-bit int and a 16-bit int. Am I doing something wrong with those parameters?
Full Traceback:
Traceback (most recent call last):
File "/home/me/myproject/utilitieslib/tests/test_mymodule.py", line 187, in test_decode_my_string
x = mymodule.decode_my_string(response)
File "/home/me/myproject/utilitieslib/mymodule.py", line 242, in decode_my_string
clib.decode_my_string(buffer, len(buffer), c_string, len(c_string))
TypeError: an integer is required

binascii fails on a2b_hqx( b2a_hqx( data ) ) with incomplete number of bytes

Using the Python binascii module, this fails:
import binascii
b = binascii.b2a_hqx( 'data' )
a = binascii.a2b_hqx( b )
with
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
binascii.Incomplete: String has incomplete number of bytes
This happens with both Python 2 and 3 (with 3, use b'data' instead). While it works fine with b2a_base64 and a2b_base64.
What am I doing wrong?
Check this out:
b = binascii.b2a_hqx( 'dataa' )
a = binascii.a2b_hqx( b )
Program exited with error
Incomplete(String has incomplete number of bytes)
b = binascii.b2a_hqx( 'dataaa' )
a = binascii.a2b_hqx( b )
https://docs.python.org/2/library/binascii.html?highlight=b2a_hqx#binascii.b2a_hqx
binascii.b2a_hqx(data)
Perform hexbin4 binary-to-ASCII translation and return the resulting string. The argument should already be RLE-coded, and have a length divisible by 3 (except possibly the last fragment).

Reading a single number from another file using python

I am reading a number from a file p2.txt. This file contrains only 1 number which is an integer lets say 10.
test_file = open('p2.txt', 'r')
test_lines = test_file.readlines()
test_file.close()
ferNum= test_lines[0]
print int(ferNum)
when however, I am getting an error
print int(ferNum)
ValueError: invalid literal for int() with base 10: '1.100000000000000000e+01\n'
I can see that it is considering it just as a line. How can I parse that number to a variable? any suggestions? regards
The problem is that even though the value of the number is an integer (11) it is represented in scientific notation so you'd have to read it as a float first.
>>> float('1.100000000000000000e+01\n')
11.0
>>> int('1.100000000000000000e+01\n')
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
int('1.100000000000000000e+01\n')
ValueError: invalid literal for int() with base 10: '1.100000000000000000e+01\n'
You can of course convert first to a float then to an int after that.
>>> int(float('1.100000000000000000e+01\n'))
11

Categories

Resources