Python Latin-1 Supplement to decimal - python

with python
ord('a')
gives
97
but
ord('é')
gives an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found
How to get the decimal for other letter than ASCII (é = 233) like in https://en.wikipedia.org/wiki/List_of_Unicode_characters#Latin-1_Supplement?

You can decode it first:
ord('é'.decode('utf-8')) # outputs 233

Related

How can I create an array of Unicode characters in Python?

I am using Repl.it.
import array as arr
my_array = arr.array("u", [u"3", u"6", u"9", u"12"])
print(my_array)
print(type(my_array))
print(type(my_array[0]))
The above source code produces the following error:
Traceback (most recent call last):
File "main.py", line 3, in <module>
my_array = arr.array("u", [u"3", u"6", u"9", u"12"])
TypeError: array item must be unicode character
Why the source code isn't working?
Array type "u" corresponds to a single Unicode character. The initializer contains a two-character item u"12". Perhaps you want something like:
arr.array("u", [u"3", u"6", u"9", u"1", u"2"])

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 3 - TypeError: can only concatenate str (not "bytes") to str

I have this wrong code and can't find the fix.
49 os.system('powershell -enc
'+base64.b64encode(addpermissions.encode('utf_16_le')))
.
.
.
137 HexRegHash, HexRegSysk, jd, skew1, gbg, data = getRegistryValues(HexRID)
I have this error:
Traceback (most recent call last):
File "hash.py", line 137, in <module>
HexRegHash, HexRegSysk, jd, skew1, gbg, data =
getRegistryValues(HexRID)
File "hash.py", line 49, in getRegistryValues
os.system('powershell -enc
'+base64.b64encode(addpermissions.encode('utf_16_le')))
TypeError: can only concatenate str (not "bytes") to str
base64.b64encode produces a byte-stream, not a string. So for your concatenation to work, you have to convert it into a string first with str(base64.b64encode(addpermissions.encode('utf_16_le')))
b64cmd = base64.b64encode(cmd.encode('utf_16_le')).decode('utf-8')
os.system('powershell -enc ' + b64cmd)
EDIT: Normal string conversion didn't work with os.system, used decode('utf-8') instead

TypeError: 'in <string>' requires string as left operand, not int

Why am I getting this error in the very basic Python script? What does the error mean?
Error:
Traceback (most recent call last):
File "cab.py", line 16, in <module>
if cab in line:
TypeError: 'in <string>' requires string as left operand, not int
Script:
import re
import sys
#loco = sys.argv[1]
cab = 6176
fileZ = open('cabs.txt')
fileZ = list(set(fileZ))
for line in fileZ:
if cab in line:
IPaddr = (line.strip().split())
print(IPaddr[4])
You simply need to make cab a string:
cab = '6176'
As the error message states, you cannot do <int> in <string>:
>>> 1 in '123'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'in <string>' requires string as left operand, not int
>>>
because integers and strings are two totally different things and Python does not embrace implicit type conversion ("Explicit is better than implicit.").
In fact, Python only allows you to use the in operator with a right operand of type string if the left operand is also of type string:
>>> '1' in '123' # Works!
True
>>>
>>> [] in '123'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'in <string>' requires string as left operand, not list
>>>
>>> 1.0 in '123'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'in <string>' requires string as left operand, not float
>>>
>>> {} in '123'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'in <string>' requires string as left operand, not dict
>>>

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