Python: Converting python2 to python3 [duplicate] - python

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

Related

Code not working anymore in the latest python version [duplicate]

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'

Invalid syntax error when using f-string in Python 3.6 [duplicate]

This question already has answers here:
Invalid Syntax when F' string dictionary [duplicate]
(3 answers)
Closed 2 years ago.
I've run into this weird error and I'm wondering whether anyone has a clue what's going on.
I'm trying to print the minimal date from a date column in Pandas series. However, the following is raising an invalid syntax error:
print(f'{df_raw['POSTING_DATE'].min()}')
This does work, though:
min_date = df_raw['POSTING_DATE'].min()
print(f'{min_date}')
Using .format() also works. Obviously I can use a workaround here but I was just wondering why the f-string syntax doesn't work in this case. I thought the f-strings should be able to handle similar expressions.
I'm using Python 3.6.9.
Use " as:
print(f"{df_raw['POSTING_DATE'].min()}")
Update:
Ideally, we could use \ to escape quotes but f-strings does not support using \ in it so this wouldn't work with f-strings
print(f'{df_raw[\'POSTING_DATE\'].min()}')

how to decide type of a variable in python? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
enter image description here
I wrote a simple code in python.
Originally my assignment is to receive two inputs(name of person) and print them.
Here's my question.
When I try to sum two variables but one of them is int and another one is str, an error occurs.
But in this case (the picture) why variable 'a' is recognized as a str not int?
I think there must occurs an error but a is recognized as a str and work well.
In Python 3, input() always returns a string. (In Python 2, input() would try to interpret – well, evaluate, actually – things, which was not a good idea in hindsight. That's why it was changed.)
If you want to (try to) make it an int, you'll need int(input(...)).

Typecasting Error in Python [duplicate]

This question already has answers here:
How to convert a string to a number if it has commas in it as thousands separators?
(10 answers)
Closed 5 years ago.
pop=x.nextSibling()[0] # pop="Population: 1,414,204"
pre_result=pop.text.split(" ") #pre_result=['Population:','1,414,204']
a=pre_result[1] # a='1,414,204'
result=int(a) #Error pops over here.Tried int(a,2) by searching answers in internet still got an error.
print(result)
print(type(result))
The following is the error message.I thought typecasting from str to int would be simple but still i came across this error.I am a beginner in python so sorry if there has been any silly mistake in my code.
File "C:\Users\Latheesh\AppData\Local\Programs\Python\Python36\Population Graph.py", line 14, in getPopulation
result=int(a)
ValueError: invalid literal for int() with base 10: '1,373,541,278'
The error is self explaining, a contains the string '1,373,541,278', and that is not a format Python can handle.
We can however remove the comma's from the string, with:
result=int(a.replace(',', ''))
But it is possible that for some elements, you will have to do additional processing.

Invalid Syntax confusion on Python Integers [duplicate]

This question already exists:
accessing a python int literals methods [duplicate]
Closed 9 years ago.
We know that everything is an object in Python and so that includes integers. So doing dir(34) is no surprise, there are attributes available.
My confusion stems from the following, why is it that doing 34.__class__ gives a syntax error when I know that 34 does have the attribute __class__. Furthermore, why does binding an integer to a name, say x, and then doing x.__class__ yield my expected answer of type int?
Because 34.__class__ is not a valid floating-point number, which is what the . denotes in a numeric literal. Try (34).__class__.

Categories