Can I use integers to use a unicode in python? - python

I have a slight problem that may be unsolvable in python.
Can you use an integer variable to type unicode in python? For example, let's say you have a variable: `variable = 0041`. Can you do something like (without having to manually type it) get `\u0041` (`a`)?
I have not really tried anything because nothing comes to mind when I try to solve this problem. If there is a possible solution, what is it?
Sorry if the answer is painfully obvious or I did not provide enough information, I'm new to this.

This is what the chr function is for.
>>> chr(int('0041', 16))
'A'

Related

Concatenate string to dictionary path when using print in Python

I want to create a variable such as 'path' and use it to get the same result when getting data from the dictionary. Is this concatenation possible by doing some type of conversions?
print(dict['detect'][1]['ssh'][0]['Server']) #Result
path = "['ssh'][0]['Server']"
print(dict['detect'][1]{path}) #...should give same result
To answer your immediate question, you can use eval() to treat the string data as code:
print(eval(f"dict['detect'][1]{path}")
That being said, be very careful when using eval(), as it introduces major security risks. I don't see a way to use ast.literal_eval() here, so if at all possible I would suggest avoiding this appoach entirely.

Missing zeros when using .format() to make an integer to a binary

So I wanted to convert an integer to a binary in Python.
And I found this really nice explanation and used it in my program.
Since I later had to work with the binary I had some issues and saw that some curious things happen with the format.
When having '{0:08b}'.format(6) in my program or entering it into the shell I get: '00000110'.
But when I try the same thing with 16 and 32 bit the zeros are not shown and I can't seem to use them.
Like:
>>> '{0:16b}'.format(6)
' 110'
>>> '{0:32b}'.format(6)
' 110'
So I wondered why it is like that (I found another solution for myself and my program but it's still annoying me to not know why it is like this!)
If this question was already asked and answered I'm sorry. I googled for it and tried to find a question like mine here with no success.
The 0 in 08b is part of the format specification. See the documentation here. If you want zero padding, do '{0:016b}'.format(6) or '{0:032b}'.format(6).

how to avoid python numeric literals beginning with "0" being treated as octal?

I am trying to write a small Python 2.x API to support fetching a
job by jobNumber, where jobNumber is provided as an integer.
Sometimes the users provide ajobNumber as an integer literal
beginning with 0, e.g. 037537. (This is because they have been
coddled by R, a language that sanely considers 037537==37537.)
Python, however, considers integer literals starting with "0" to
be OCTAL, thus 037537!=37537, instead 037537==16223. This
strikes me as a blatant affront to the principle of least
surprise, and thankfully it looks like this was fixed in Python
3---see PEP 3127.
But I'm stuck with Python 2.7 at the moment. So my users do this:
>>> fetchJob(037537)
and silently get the wrong job (16223), or this:
>>> fetchJob(038537)
File "<stdin>", line 1
fetchJob(038537)
^
SyntaxError: invalid token
where Python is rejecting the octal-incompatible digit.
There doesn't seem to be anything provided via __future__ to
allow me to get the Py3K behavior---it would have to be built-in
to Python in some manner, since it requires a change to the lexer
at least.
Is anyone aware of how I could protect my users from getting the
wrong job in cases like this? At the moment the best I can think
of is to change that API so it take a string instead of an int.
At the moment the best I can think of is to change that API so it take a string instead of an int.
Yes, and I think this is a reasonable option given the situation.
Another option would be to make sure that all your job numbers contain at least one digit greater than 7 so that adding the leading zero will give an error immediately instead of an incorrect result, but that seems like a bigger hack than using strings.
A final option could be to educate your users. It will only take five minutes or so to explain not to add the leading zero and what can happen if you do. Even if they forget or accidentally add the zero due to old habits, they are more likely to spot the problem if they have heard of it before.
Perhaps you could take the input as a string, strip leading zeros, then convert back to an int?
test = "001234505"
test = int(test.lstrip("0")) # 1234505

Why string method on a string object doesn't modify the object in Python?

I found it odd that a string method operation on a string object doesn't modify the string object. Why is it? I wasted quite a bit of time yesterday trying to understand why my code wasn't working when I finally discovered this.
Strings are immutable types in python. Main advantages of being immutable would be:
simplify multithreaded programming.
can be used as dictionary keys (will keep the same hash)
Strings are immutable by design in Python. This is common to many other languages, too, so it's not a Python-specific thing. For the "Why?" please see these excellent answers here on SO, and also this great blog post by Eric Lippert.
That's why string operations always return a new string (which you then may re-assign to the same name as before like
mystr = mystr.upper()

How would one convert a Python string representation of a byte-string to an actual byte-string? [duplicate]

This question already has an answer here:
Converting python string into bytes directly without eval()
(1 answer)
Closed 4 years ago.
I'm trying to figure out how one might convert a string representation of a byte-string into an actual byte-string type. I'm not very used to Python (just hacking on it to help a friend), so I'm not sure if there's some easy "casting" method (like my beloved Java has ;) ). Basically I have a text file, which has as it's contents a byte-string:
b'\x03\xacgB\x16\xf3\xe1\\v\x1e\xe1\xa5\xe2U\xf0g\x956#\xc8\xb3\x88\xb4E\x9e\x13\xf9x\xd7\xc8F\xf4'
I currently read in this file as follows:
aFile = open('test.txt')
x = aFile.read()
print(x) # prints b'\x03\xacgB\x16\xf3\xe1\\v\x1e\xe1\xa5\xe2U\xf0g\x956#\xc8\xb3\x88\xb4E\x9e\x13\xf9x\xd7\xc8F\xf4'
print(type(x)) # prints <class 'str'>
How do I make x be of type <class 'bytes'>? Thanks for any help.
Edit: Having read one of the replies below, I think I'm maybe constraining the question too much. My apologies for that. The input string doens't have to be in python byte-string format (i.e. with the b and the quotation marks), it could just be the plain byte-string:
\x03\xacgB\x16\xf3\xe1\\v\x1e\xe1\xa5\xe2U\xf0g\x956#\xc8\xb3\x88\xb4E\x9e\x13\xf9x\xd7\xc8F\xf4
If this makes it easier or is better practice, I can use this.
>>> r'\x03\xacgB\x16\xf3\xe1\\v\x1e\xe1\xa5\xe2U\xf0g\x956#\xc8\xb3\x88\xb4E\x9e\x13\xf9x\xd7\xc8F\xf4'.decode('string-escape')
'\x03\xacgB\x16\xf3\xe1\\v\x1e\xe1\xa5\xe2U\xf0g\x956#\xc8\xb3\x88\xb4E\x9e\x13\xf9x\xd7\xc8F\xf4'
This will work for strings that don't have b'...' around it. Otherwise you are encouraged to use ast.literal_eval().
Since your input is in Python's syntax, for some reason (*), the thing to do here is just call eval:
>>> r"b'\x12\x12'"
"b'\\x12\\x12'"
>>> eval(r"b'\x12\x12'")
'\x12\x12'
Be careful, though, as this may be a security problem. eval will run any code, so you may need to sanitize the input. In your case its simple - just check that the thing you're eval-ing is indeed a string in the format you expect. If security isn't an issue here, just don't bother.
Redarding your EDIT: Still, eval is the simplest approach here (after adding the b'' if it's not there). You could also, of course, do this manually by converting each \xXX to its real value.
(*) Why, really? This seems like a strange choice for a data representation format

Categories

Resources