Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python将列表转换为Int_Python_Xpath_Int_Type Conversion - Fatal编程技术网

使用Python将列表转换为Int

使用Python将列表转换为Int,python,xpath,int,type-conversion,Python,Xpath,Int,Type Conversion,我已经在这里复习了一些问题来做这件事,但是运气不好 我正在尝试将变量电话号码从列表转换为int,以便将其正确插入数据库列int(10),例如888112222 我目前有: 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

我已经在这里复习了一些问题来做这件事,但是运气不好

我正在尝试将变量
电话号码
列表
转换为
int
,以便将其正确插入数据库列int(10),例如888112222

我目前有:

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
这输出的数字没有
-
就可以了

问题是,我希望这些值是
int
形式,但我仍然相信它们的
list
形式(没有破折号,我怎么做?

因为
xpath()
返回一个字符串列表,并且您应用-
电话号码
肯定是一个字符串,您可以使用以下方法将其转换为
int

如果
xpath()
调用只返回一项,只需获取第一个元素:

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

.replace(“”,”)
的意义是什么?非常彻底。非常感谢@alecxe!
phone_number = toasted_tree.xpath('//b/text()')[0]
phone_number = int(phone_number.replace('-', ''))