pop=x.nextSibling()[0] # pop="Population: 1,414,204"
pre_result=pop.text.split(" ") #pre_result=['Population:','1,414,204']
a=pre_result[1] # a='1,414,204'
result=int(a) #Error pops over here.Tried int(a,2) by searching answers in internet still got an error.
print(result)
print(type(result))
以下是错误消息。我认为从str到int的类型转换很简单,但我仍然遇到了这个错误。我是python的初学者,很抱歉,如果我的代码中有任何愚蠢的错误。
File "C:\Users\Latheesh\AppData\Local\Programs\Python\Python36\Population Graph.py", line 14, in getPopulation
result=int(a)
ValueError: invalid literal for int() with base 10: '1,373,541,278'
答案 0 :(得分:3)
错误是自我解释,a
包含字符串'1,373,541,278'
,这不是Python可以处理的格式。
然而,我们可以从字符串中删除逗号:
result=int(a.replace(',', ''))
但是对于某些元素,您可能需要进行额外的处理。