0

When trying to convert a string into integer to be used as a variable later in the code, I get the following:

print int(urlsuccessful[i])

ValueError: invalid literal for int() with base 10: '2,919,247'
1
  • how about getting rid of commas? Commented Apr 14, 2011 at 18:44

4 Answers 4

4

locale.atoi() will "demark" integers based on the current locale setting.

Sign up to request clarification or add additional context in comments.

Comments

3

If only problems are commas, try:

>>> int("2,919,247".replace(",", ""))
2919247

1 Comment

One can also: #urlsuccessful[i] = re.sub(",","",urlsuccessful[i]) #urlerrors[i] = re.sub(",","",urlerrors[i])
1

int does not understand commas, you'll want to remove those before trying to convert

Comments

-1

You can just do

def int2str(my_integer):
  return "%d" % my_integer

1 Comment

Elaboorating on @Ignacio Vazquez-Abrams' comment: the return here will convert an integer to a string (and you could do that more easily with str()), but the OP wants to convert a string to an int.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.