This question already has answers here:
What's the correct way to convert bytes to a hex string in Python 3?
(9 answers)
Closed last year.
I have this code:
cmd_login = '534d4100000402a000000001003a001060650ea0ffffffffffff00017800%s00010000000004800c04fdff07000000840300004c20cb5100000000%s00000000' % (struct.pack('<I', src_serial).encode('hex'), get_encoded_pw(user_pw))
written for python 2.7.
I don't find how to change this, to get it working in python 3.9. I get the error: 'bytes' object has no attribute 'encode'.
The struct.pack function returns a bytes object. If you want to convert that to a hex string it has direct support for that using its hex() method.
data = b"abcde"
data.hex()
Produces:
'6162636465'
Related
This question already has answers here:
Difference between int() and long()
(1 answer)
What are metaclasses in Python?
(25 answers)
Closed 1 year ago.
I am trying to use
https://github.com/iandees/mongosm/blob/master/insert_osm_data.py
this package. It seems like it is written in Python2. I have converted all the way to next(context). However, I am getting name 'long' is not defined.
Is there any way that I can define this somewhere? How can I define 'long' and I have no idea what this is for even for Python2 Script (which worked fine somehow).
long() is basically renamend to int() in Python 3.
Please see https://www.python.org/dev/peps/pep-0237/ for details.
So, either do a search of long and replace with int, or define it
long = int
somewhere at the beginning of your file.
You should convert all code to Python3
https://www.google.com/search?channel=fs&client=ubuntu&q=Converting+python2+to+python3
https://docs.python.org/3/library/2to3.html
This question already has an answer here:
AttributeError: 'str' object has no attribute 'isnumeric'
(1 answer)
Closed 2 years ago.
I entered this code on my text editor and it results in an error. So, do I need to download any kind of files for my Python edition in order to use this isnumeric method?
txt = "565543"
x = txt.isnumeric()
print(x)
AttributeError: 'str' object has no attribute 'isnumeric'
isnumeric is a inbuilt function. maybe due to your python version or python files the function is not working. try installing python 3.6.8, it will work now and i have tried.
More than likely you are using an older version of Python.
In older versions of Python isnumeric() only works on Unicode objects
Try running the code this way:
txt = u"565543"
x = txt.isnumeric()
print(x)
This question already has answers here:
Python sockets error TypeError: a bytes-like object is required, not 'str' with send function
(4 answers)
Closed 3 years ago.
I want to send some inputs to a cmd based programme using python. The program takes normally type inputs from keyboard.
I tried as:
P1=subprocess.Popen("my_program",stdin=subprocess.PIPE,stdout=subprocess.PIPE,sterr=subprocess.PIPE)
p1out,p1err=P1.communicate(input="my_input")
But gave error as "it requires byte like object not str". I had also tried with P1.stdin.write() method and gave same error again. What should be my input dtype?
It seems like all you have to change is your string to a 'bytes' type.
type("my_input")
>>>> str
type(b'my_input')
>>>> bytes
I solved using
my_input=my_input.encode("utf-8")
This question already has an answer here:
Python: Converting HEX string to bytes
(1 answer)
Closed 2 years ago.
I am using python 3 and try to convert a hex-string to a byte-represented form. So i used the following command:
bytes.fromhex('97ad300414b64c')
I Expected results like this: b'\x97\xad\x30\x04\x14\xb6\x4c'' but got b'\x97\xad0\x04\x14\xb6L'. I am note sure what i am doing wrong, but maybe it is something with the encoding?
As pointed by #user8651755 in the comments, this is due to the fact that some bytes correspond to printable characters. So the answer is: you are doing everything right.
This question already has answers here:
when convert to base 64, TypeError: 'str' does not support the buffer interface [duplicate]
(2 answers)
Why do I need 'b' to encode a string with Base64?
(5 answers)
Closed 5 years ago.
I've taken a look at many other "Questions that may already have your answer", but didn't come across any that hit directly on the issue I'm running into.
Here's my Python 2 snippet:
api_access_token = base64.b64encode('%s:' % api_access_token_setup)
I am getting the following error:
TypeError: 'str' does not support the buffer interface
Any chance there's a quick fix for solving the error being prompted.
As the error suggests, you need to pass bytes, not str type to b64encode. Try encoding your string:
api_access_token = base64.b64encode(('%s:' % api_access_token_setup).encode())