Converting the received data into binary form in python - python

I want to convert the received hex data into binary form. I get ValueError.
For example, I want the first value in the output to be printed as 0000.
received_data = " ".join("{:02x}".format(byte) for byte in (data))
print(received_data)
P_data = "{0:b}".format(received_data[0:1])
Output:
01 04 04
Error:
Traceback (most recent call last):
File "C:\Users\User\eclipse-workspace\Try\test1\test2.py", line 22, in
<module>
P_data="{0:b}".format(received_data[0:1])
ValueError: Unknown format code 'b' for object of type 'str'

You should first convert your string into an integer
P_data = '{0:b}'.format(int(received_data[0:1], 16)).zfill(4)

Related

TypeError: a bytes-like object is required, not 'str' while using re.replace

I do not really understand all this encode stuff. I am not sure how to solve the error below. Would really appreciate if someone could help me with this.
string = ' frame #0: [03m0x00067[0m embedded`excenerate_trap at [36mcpptions.c[0m:[36m398[0m:[33m3[0m [anu]!'
# print string
print('The string is:', string)
# ignore error
print('The encoded version (with ignore) is:', string.encode('utf-8'))
# replace error
string = string.encode("unicode_escape")
print('The encoded version (with replace) is:', string)
string = string.replace("'\\\\0", "'\\\\x00")
print("\n", string , "\n")
ERROR that I get:
The string is: frame #0: 0x00067 embedded`excenerate_trap at cpptions.c:398:3 [anu]!
The encoded version (with ignore) is: b' frame #0: \x1b[03m0x00067\x1b[0m embedded`excenerate_trap at \x1b[36mcpptions.c\x1b[0m:\x1b[36m398\x1b[0m:\x1b[33m3\x1b[0m [anu]!'
The encoded version (with replace) is: b' frame #0: \\x1b[03m0x00067\\x1b[0m embedded`excenerate_trap at \\x1b[36mcpptions.c\\x1b[0m:\\x1b[36m398\\x1b[0m:\\x1b[33m3\\x1b[0m [anu]!'
Traceback (most recent call last):
File "<string>", line 15, in <module>
TypeError: a bytes-like object is required, not 'str'

words into numbers scrape

When I scrape a webpage it returns this: 42,834.56
Apparently, it extracts it as a word (because when I try to sum it whit the other ones retrieve in excel it doesn't work). How can I convert it into a number?
I'm copying from the cmd:
Here is an error when I write it with int():
Traceback (most recent call last):
File "C:\Users\Windows\Desktop\py4e\callao.py", line 337, in <module>
print(int(peso))
ValueError: invalid literal for int() with base 10: '42,834.56\xa0'
Here is an error when I write it with float():
Traceback (most recent call last):
File "C:\Users\Windows\Desktop\py4e\callao.py", line 337, in <module>
print(float(peso))
ValueError: could not convert string to float: '42,834.56\xa0'
You might need to remove ',' from the number. Try this:
float("".join(peso.split(',')))
a = "42,834.56"
b = float(a.replace(",",""))
print(type(b))
# output: <class 'float'>
You can try this.
Strip off the \xa0 character.
Remove the , from the string
Convert it to float.
s = '42,834.56\xa0'
s = s.strip()
print(float(s.replace(',','')))
You can store the scraped value in a variable and convert to float in python. Here's the function:
def convert_to_float(peso):
return float(peso.replace(',',''))
And here's the call to the function:
peso = '42,834.56'
convert_to_float(peso)
Output:
42834.56
Now you can sum it with others.
Edit -
It seems you have scraped \xa0 also along with the string. So to handle that:
def convert_to_float(peso):
peso = peso.split("\\")[0]
return float(peso.replace(',',''))
peso = '42,834.56\xa0'
convert_to_float(peso)
Output will be same as above.

How to decode hexadecimal string with "b" at the beggining?

I have an hexadecimal string like this:
s = '\x83 \x01\x86\x01p\t\x89oA'
I decoded to hex values like this, getting the following output.
>>> ' '.join('{:02x}'.format(ord(ch)) for ch in s)
'83 20 01 86 01 70 09 89 6f 41'
But now I have issues to decode a hex string that is exactly as the previous one, but this comes from a binary file. and has a b at the begining. The error below:
with open('file.dat', 'rb') as infile:
data = infile.read()
>>> data
b'\x83 \x01\x86\x01p\t\x89oA'
>>> ' '.join('{:02x}'.format(ord(ch)) for ch in data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <genexpr>
TypeError: ord() expected string of length 1, but int found
How would be the way to fix this? Thanks
Use .hex() method on the byte string instead.
In [25]: data = b'\x83 \x01\x86\x01p\t\x89oA'
In [26]: data.hex()
Out[26]: '83200186017009896f41'

Python string to timestamp

I am trying to convert these strings to timestamps:
python test.py
2015-02-15T14:25:54+00:00
2015-02-15T16:59:01+00:00
2015-02-15T18:44:13+00:00
2015-02-15T18:45:24+00:00
2015-02-15T18:52:11+00:00
2015-02-15T18:52:33+00:00
2015-02-15T18:59:00+00:00
2015-02-15T19:06:16+00:00
2015-02-15T19:07:02+00:00
I get this output on executing below code:
for member in members_dict['members']:
s = member['timestamp_signup']
print s
But when I try to get the timestamp:
for member in members_dict['members']:
s = member['timestamp_signup']
print s
print time.mktime(datetime.datetime.strptime(s, "%Y-%m-%dT%H:%M:%S+00:00").timetuple())
I get the error as:
Traceback (most recent call last):
File "test.py", line 20, in <module>
print datetime.strptime(s, '"%Y-%m-%dT%H:%M:%S+00:00"').date()
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '' does not match format '"%Y-%m-%dT%H:%M:%S+00:00"'
What am I doing wrong here?
Your code to convert string to datetime is fine. For example:
>>> from datetime import datetime
>>> my_str = '2015-02-15T14:25:54+00:00'
>>> datetime.strptime(my_str, "%Y-%m-%dT%H:%M:%S+00:00")
datetime.datetime(2015, 2, 15, 14, 25, 54)
Error you are getting is due to empty string present in your file. I got to know about it based on your error message:
ValueError: time data '' does not match format
# empty string ^
Possibly there is empty line at the end of your file (or, somewhere else)

python convert string "0" to float gives error

i have the following code in my python script, to launch an application and grab the output of it.
An example of this output would be 'confirmed : 0'
Now i only want to know the number, in this case zero, but normally this number is float, like 0.005464
When i run this code it tells me it cannot convert "0" to float. What am i doing wrong?
This is the error i get now:
ValueError: could not convert string to float: "0"
cmd = subprocess.Popen('/Applications/Electrum.app/Contents/MacOS/Electrum getbalance', shell=True, stdout=subprocess.PIPE)
for line in cmd.stdout:
if "confirmed" in line:
a,b=line.split(': ',1)
if float(b)>0:
print "Positive amount"
else:
print "Empty"
According to the exception you got, the value contained in b is not 0, but "0" (including the quotes), and therefore cannot be converted to a float directly. You'll need to remove the quotes first, e.g. with float(b.strip('"')).
As can be seen in the following examples, the exception description does not add the quotes, so they must have been part of the original string:
>>> float('"0"')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: "0"
>>> float('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: a
I have tested the code and found that split(': ', 1) result contains string
>>> line = "1: 456: confirmed"
>>> "confirmed" in line
True
>>> a,b=line.split(': ', 1)
>>> a
'1'
>>> b
'456: confirmed'

Categories