Reputation: 823
I'm using int(myString), myString = "-1" and I get error:
ValueError: invalid literal for int() with base 10: '"-1"'
Upvotes: 1
Views: 7316
Reputation: 59674
str.strip method accepts characters. You can use it to get rid of surrounding quotes:
>>> int('"-1"'.strip('"'))
-1
>>>
Upvotes: 1
Reputation: 24163
>>> int("-1")
-1
>>> int('"-1"')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
int('"-1"')
ValueError: invalid literal for int() with base 10: '"-1"'
>>> '"-1"'.strip('\"')
'-1'
Upvotes: 1
Reputation: 26189
The string contains quotes, i.e.
s = '"-1"'
You need to get rid of the quotes, something like
s = '"-1"'
int(s.replace('"', ''))
should do the trick.
Upvotes: 11
Reputation: 5616
Are you sure that you're string doesent look like "'-1'"
and not "-1"
a = "'-1'"
print int(a)
>>> ValueError: invalid literal for int() with base 10: '"-1"'
a = "-1"
print int(a)
>>> -1
Upvotes: 2