Cmag
Cmag

Reputation: 15750

Converting string to integer with python

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'

Upvotes: 0

Views: 376

Answers (4)

utdemir
utdemir

Reputation: 27216

If only problems are commas, try:

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

Upvotes: 3

oceanhug
oceanhug

Reputation: 1392

You can just do

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

Upvotes: -1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798436

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

Upvotes: 4

Daniel DiPaolo
Daniel DiPaolo

Reputation: 56390

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

Upvotes: 1

Related Questions