0

I'm using some library which converts data to other format. As I don't want to debug this library, I'd like to solve the solution on my code.

The input is integer and maybe -1. In this case and the library output is - 1. I need to compare this integer if this is positive or negative. How can I handle the - 1 for this check?

2 Answers 2

1

If you have a string x="- 1" and want to convert it to int, eval(x) works.

Python 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = "- 1"
>>> intx = eval(x)
>>> print(type(intx))
<class 'int'>
>>> print(intx)
-1
Sign up to request clarification or add additional context in comments.

1 Comment

at least use ast.literal_eval its marginally safer :) good answer though
0
int("- 1") # nvm this doesnt work in py3

i think works (but not float('- 1') )

you could also just remove all the spaces

float("- 1".replace(" ",""))

1 Comment

lol weird it works in py2 ... i guess they got rid of that in py3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.