CodeTalk CodeTalk - 1 month ago 5
Python Question

Convert List to Int with Python

I've reviewed a few questions already here to do this, but with no luck.

I'm trying to convert the variable

from a
to a
in order to properly insert it into a database column INT(10) e.g. 8881112222

I currently have:

phone_numbers = toasted_tree.xpath('//b/text()')
#convert phone number 888-111-2222 to 8881112222
for phone_number in phone_numbers:
phone_number = phone_number.replace('-', '').replace('','')
print phone_number

This outputs numbers without a
just fine.

The problem is, I want these values to be a
form and I still believe their in
form (just without dashes, How can I do this?


Since xpath() returns a list of strings and you apply .replace() - phone_number is definitely a string, you can convert it to int using int():

phone_number = int(phone_number)

In case there is only one item returned by the xpath() call, just get the first element:

phone_number = toasted_tree.xpath('//b/text()')[0]
phone_number = int(phone_number.replace('-', ''))