"invalid literal" from int(base64.urlsafe_b64decode(mystr)) - python

I have a base64 string that I need to decode, then i convert it into a integer so I can "% 2" it. the base64 decode is easy but apparently I have some confusion on how python actually handles binary:
>>> y = 'EFbSUq0g7qvoW2ehykfSveb_pSmunxOJUEVao1RWwck'
>>> int(base64.urlsafe_b64decode('EFbSUq0g7qvoW2ehykfSveb_pSmunxOJUEVao1RWwck='), 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 2: b'\x10V\xd2R\xad \xee\xab\xe8[g\xa1\xcaG\xd2\xbd\xe6\xff\xa5)\xae\x9f\x13\x89PEZ\xa3TV\xc1\xc9'
>>> int(base64.urlsafe_b64decode('EFbSUq0g7qvoW2ehykfSveb_pSmunxOJUEVao1RWwck='), 16)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 16: b'\x10V\xd2R\xad \xee\xab\xe8[g\xa1\xcaG\xd2\xbd\xe6\xff\xa5)\xae\x9f\x13\x89PEZ\xa3TV\xc1\xc9'
>>>

Use int.from_bytes() to convert a base64 decoded string into int
int.from_bytes(
base64.urlsafe_b64decode(
'EFbSUq0g7qvoW2ehykfSveb_pSmunxOJUEVao1RWwck='
),
'big' # the endianness
)
7390406020584230016520446236832857473226268177813448430255309703833393217993

Related

TypeError: a bytes-like object is required, not 'str' : Getting this error

Getting error : Conflict between bytes and str type
old_server_uuid = p3.communicate()[0].split("|")[1].strip()
Getting the below error:
Traceback (most recent call last):
File "create_env_file.py", line 68, in <module>
data = create_env_source_list(node_name, ip_address)
File "create_env_file.py", line 14, in create_env_source_list
raise(ex)
File "create_env_file.py", line 12, in create_env_source_list
old_server_uuid = p3.communicate()[0].split("|")[1].strip()
TypeError: a bytes-like object is required, not 'str'
Python 3 doesn't allow mixing text (Unicode) strings and byte strings.
the result of p3.communicate()[0] is a byte string, so the .split argument must be one as well.
Examples:
Splitting a byte string with a Unicode string:
>>> b'abc|123'.split('|')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
Splitting a byte string with a byte string:
>>> b'abc|123'.split(b'|')
[b'abc', b'123']

convert unicode string to float

I have the Python string u'\u221220' aka "−20" with the Unicode minus sign.
When trying to convert into a float, I'm getting
>>> a = u'\u221220'
>>> float(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'decimal' codec can't encode character u'\u2212' in position 0: invalid decimal Unicode string
with Python 2 and
>>> a = u'\u221220'
>>> float(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '−20'
with Python 3.
How can I properly convert u'\u221220' into the float -20.0 in both Python 2 and Python 3? A portable solution would be great.
From #j-f-sebastian:
a = u'\u221220'
float(a.replace(u'\N{MINUS SIGN}', '-'))
does the trick. See the related Python issue.

python convert string like ['-0.88854965D+02'] to float

How to convert the string like '-0.88854965D+02' to float?
float('-0.88854965D+02') doesn't work:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): -0.88854965D+02
The string '-0.88854965D+02' appears to be written as scientific E notation. If that is the case, then you can use the following one-liner to replace the 'D' with 'E', and then convert to a float:
n = float('-0.88854965D+02'.replace("D", "E"))
print n
-88.854965

Type error in Python: need a single Unicode character as parameter

When I try to convert a unicode variable to float using unicodedata.numeric(variable_name), I get this error "need a single Unicode character as parameter". Does anyone know how to resolve this?
Thanks!
Here is the code snippet I'm using :
f = urllib.urlopen("http://compling.org/cgi-bin/DAL_sentence_xml.cgi?sentence=good")
s = f.read()
f.close()
doc = libxml2dom.parseString(s)
measure = doc.getElementsByTagName("measure")
valence = unicodedata.numeric(measure[0].getAttribute("valence"))
activation = unicodedata.numeric(measure[0].getAttribute("activation"))
This is the error I'm getting when I run the code above
Traceback (most recent call last):
File "sentiment.py", line 61, in <module>
valence = unicodedata.numeric(measure[0].getAttribute("valence"))
TypeError: need a single Unicode character as parameter
Summary: Use float() instead.
The numeric function takes a single character. It does not do general conversions:
>>> import unicodedata
>>> unicodedata.numeric('½')
0.5
>>> unicodedata.numeric('12')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: need a single Unicode character as parameter
If you want to convert a number to a float, use the float() function.
>>> float('12')
12.0
It won't do that Unicode magic, however:
>>> float('½')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '½'

QString: Unicode encoding-decoding problem

I am trying to make a simple conversion to Unicode string to standart string, but no success.
I have: PyQt4.QtCore.QString(u'\xc5\x9f')
I want: '\xc5\x9f' notice str type not unicode, because the library I am using is not accepting unicode.
Here is what I tried, you can see how hopeless I am :) :
>>> s = QtCore.QString(u'\xc5\x9f')
>>> str(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) '
>>> s.toUtf8()
PyQt4.QtCore.QByteArray('\xc3\x85\xc2\x9f')
>>> s.toUtf8().decode("utf-8")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'QByteArray' object has no attribute 'decode'
>>> str(s.toUtf8()).decode("utf-8")
u'\xc5\x9f'
>>> str(str(s.toUtf8()).decode("utf-8"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
I know there are a lot of questions related to Unicode, but I can't find this answer.
What should I do?
Edit:
I found a hacky way:
>>> unicoded = str(s.toUtf8()).decode("utf-8")
>>> unicoded
u'\xc5\x9f'
>>> eval(repr(unicoded)[1:])
'\xc5\x9f'
Do you know a better way?
If you have unicode string of QString data type , and need to convert it to python string , you just :
unicode(YOUR_QSTRING_STRING)
Is this what you are after?
In [23]: a
Out[23]: u'\xc5\x9f'
In [24]: a.encode('latin-1')
Out[24]: '\xc5\x9f'
In [25]: type(a.encode('latin-1'))
Out[25]: <type 'str'>

Categories

Resources