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)
Related
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'
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 answers here:
Differences between `input` and `raw_input` [duplicate]
(3 answers)
Closed 2 years ago.
I'm just starting to learn basic python coding at the moment and I'm using a video tutorial to do so, I'm running IDLE Python 2.7.12 Shell and following along with the instructor using IDLE 3.5.0 Shell. So the issue I've come across is when I'm trying to use the split method this is coming up
>>> numbers = input("Enter your numbers, separated by commas: ")
Enter your numbers, separated by commas: 1,2,3
>>> numbers.split(",")
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
numbers.split(",")
AttributeError: 'tuple' object has no attribute 'split'
I can't really figure out why that is. When he runs it the numbers come back ['1','2','3']. I'm sure this is really basic but I would greatly appreciate any help or advice you can offer. Thanks.
You are using python2 and in this case input() evaluates the user input. In python2 you should use raw_input() instead of input()
That said, you should really be using python 3 (that is what your instructor is using)
word=raw_input("Test")
word.split()
print(word)
Use raw_input() for Python 2.X. It has been deprecated in Python 3.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 answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 4 years ago.
coord = {'latitude' : '37.24N' , 'longitude' : '-115.81'}
print 'coordinates: {latitude}, {longitude}' .format(**coord)
I've been recently following this new program on learning python and I've come across an error that says SyntaxError: invalid syntax,but I can't spot any syntax errors, if anyone can offer help it would be much appreciated.
I'm guessing that you are getting a syntax error related to the print statement if you are using the wrong version of Python. Make sure you are using Python 2.x, otherwise upgrade your code to be compatible with Python 3.x.
python --version should tell you which version you are using
Python 3.x version of your code. Notice the round brackets used with the print statement.
coord = {'latitude' : '37.24N' , 'longitude' : '-115.81'}
print('coordinates: {latitude}, {longitude}'.format(**coord))