bytes, strings and urllib in Python3 - python-3.x

I'm having trouble wrapping my brain around this error. It's really basic, but it seems to say the opposite of what is true.
>>> x=b'hi'
>>> urllib.parse.unquote(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/parse.py", line 609, in unquote
if '%' not in string:
TypeError: a bytes-like object is required, not 'str'
I've passed the code a bytes object, I would hope that's bytes-like enough. urllib.parse.unquote() seems to only work with a str object.
So why would it generate an error saying it needs a bytes-like object?

I suggest to use urllib.parse.unquote_to_bytes, which encodes its string parameter to bytes if it receives a str object.
urllib.parse.unquote_to_bytes(x)

Related

Converting list of string to bytes in Python3

I have been trying to convert a list of string elements to bytes so that I can send it to the server.
Below is the snippet for my code:-
ls_queue=list(q.queue)
print("Queue converted to list elements:::",ls_queue)
encoded_list=[x.encode('utf-8') for x in ls_queue]
print("Encoded list:::",encoded_list)
s.send(encoded_list)
The output I get is:
Encoded list::: [b'madhav']
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in
__call__
return self.func(*args)
File "Practice_Client_Server.py", line 149, in Word_Append_Window
s.send(encoded_list)
TypeError: a bytes-like object is required, not 'list'
I can see that it is getting converted to bytes but it still gives the error while trying to encode and send. Can someone take a look as to what I am doing wrong here?
Thank you
You are sending a list object when send is expecting a bytes one, that happened when u converted the elements of the list into bytes but not the list container. What u can do is serialize it as a JSON string and then convert it to bytes, for example:
import json
l = ['foo', 'bar']
l_str = json.dumps(l)
l_bytes = l_str.encode('utf-8')
send(l_bytes)
Then u can read it on your server doing the opposite:
reconstructed_l = json.loads(l_bytes.decode('utf-8'))

how to remove 'int' object is not callable

how to resolve 'int' object is not callable
I am new over here
-68+(((68)**2-4(34)(-510))**0.5)/(2*34)
Traceback (most recent call last):
File "", line 1, in
-68+(((68)**2-4(34)(-510))**0.5)/(2*34)
TypeError: 'int' object is not callable
there is no output only the error message written above. How to resolve this error message?
Python does not support multiplication through parentheses (like (34)(-510) and 4(34)). Change this to (34) * (-510). So, your full line would be:
>>> -68+(((68)**2-4*(34)*(-510))**0.5)/(2*34)
-64.0
When you say 4(34), you're actually telling the interpreter to call the function named 4 with the argument 34. (This is the same syntax as saying a(34), where a is a function.) The error is because 4 is not a function, so you can't call it.
If you don't work in the python interpreter directly, you need to do something with your calcul, like putting it in a variable like this :
a = -68+(((68)**2-4*(34)*(-510))**0.5)/(2*34)
or print it :
print(-68+(((68)**2-4*(34)*(-510))**0.5)/(2*34))
Also you can't do maths with two values into parenthesis, you need to include a '*'.
Finally, doing 4(34) is like you're calling a function named "4", with an argument (34).

I don't understand the error traceback gives me

I'm currently trying to understand Pyth. I know Python quite well, but I don't get Pyth sometimes.
My code:
DhNKlNFzrKZaY#Nz;Y;hQ
It's just a basic script for reversing string, and traceback gives me ValueError,
ValueError: malformed node or string: <_ast.Name object at 0x7ff2fde45c18>
Despite my Python knowledge I have no idea what does this error mean. Can you show me where is this error coming from?
I assume you are getting an error like this one here:
Traceback (most recent call last):
File "pyth.py", line 771, in <module>
File "<string>", line 3, in <module>
File "/app/macros.py", line 691, in eval_input
File "/app/.heroku/python/lib/python3.4/ast.py", line 84, in literal_eval
File "/app/.heroku/python/lib/python3.4/ast.py", line 83, in _convert
ValueError: malformed node or string: <_ast.Name object at 0x7fac26eb2b70>
First off, you use z and Q inconsistently. With your current code, input should have been taken like this instead:
"abcd"
abcd
When Q is used in a Pyth program, z implicitly jumps to the next line of input, it just skips whatever has been inputted before using Q. Instead, just use:
DhNKlNFzrKZaY#Nz;Y;hz
And the errors should go away.
I am not sure why you would want to perform string reversal that way, though. I use _z if the input is not quoted and _ alone otherwise, since Q is implicit at the end of any Pyth program.

using str() function as variable name [duplicate]

This question already has answers here:
Python: function and variable with the same name
(3 answers)
Closed 6 years ago.
I'm newbie in python.
As I learned, str(123) returns the string format of value 123 which is '123'
but what if we have a variable named str, how can I call the str function?
In[2]: str(123)
Out[2]: '123'
In[3]: str='hello world'
In[4]: str(123)
Traceback (most recent call last):
File "/Users/x625/anaconda/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-6d97c31da288>", line 1, in <module>
str(123)
TypeError: 'str' object is not callable
How can I call str() function again?
You still can access it:
import builtins
builtins.str(123)
Don't use str as a variable name, it is a keyword.
Just don't call your variables str.
If you made a mistake in interactive mode and need to get rid of a variable you picked a bad name for, you can do del str, but don't do that in an actual program.
Your variable name str='hello world' collides withe the function str.
Just pick another name to your variable.

Python 3.2 TypeError - can't figure out what it means

I originally put this code through Python 2.7 but needed to move to Python 3.x because of work. I've been trying to figure out how to get this code to work in Python 3.2, with no luck.
import subprocess
cmd = subprocess.Popen('net use', shell=True, stdout=subprocess.PIPE)
for line in cmd.stdout:
if 'no' in line:
print (line)
I get this error
if 'no' in (line):
TypeError: Type str doesn't support the buffer API
Can anyone provide me with an answer as to why this is and/or some documentation to read?
Much appreciated.
Python 3 uses the bytes type in a lot places where the encoding is not clearly defined. The stdout of your subprocess is a file object working with bytes data. So, you cannot check if there is some string within a bytes object, e.g.:
>>> 'no' in b'some bytes string'
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
'no' in b'some bytes string'
TypeError: Type str doesn't support the buffer API
What you need to do instead is a test if the bytes string contains another bytes string:
>>> b'no' in b'some bytes string'
False
So, back to your problem, this should work:
if b'no' in line:
print(line)

Resources