Python 无法将字典元素(字符串)转换为int
在将字典中元组中的两个元素转换为int值时有点困难。字典的关键字是国家名称,信息元组是(地区、人口)。这就是我到目前为止所做的:Python 无法将字典元素(字符串)转换为int,python,converter,Python,Converter,在将字典中元组中的两个元素转换为int值时有点困难。字典的关键字是国家名称,信息元组是(地区、人口)。这就是我到目前为止所做的: def _demo_fileopenbox(): msg = "Pick A File!" msg2 = "Select a country to learn more about!" title = "Open files" default="*.py" f = fileopenbox(msg,title,d
def _demo_fileopenbox():
msg = "Pick A File!"
msg2 = "Select a country to learn more about!"
title = "Open files"
default="*.py"
f = fileopenbox(msg,title,default=default)
writeln("You chose to open file: %s" % f)
countries = {}
with open(f,'r') as handle:
reader = csv.reader(handle, delimiter = '\t')
for row in reader:
countries[row[0]] = (row[1].replace(',', ''), row[2].replace(',', ''))
for i in countries:
int((countries[i])[0])
int((countries[i])[1])
#while 1:
# reply = choicebox(msg=msg2, choices= list(countries.keys()) )
# writeln(reply + "-\tArea: " + (countries[reply])[0] + "\tPopulation: " + (countries[reply])[1] )
但我一直在犯这样的错误:
int((countries[i])[0])
ValueError: invalid literal for int() with base 10: ''
有没有办法解决这个问题或更好的方法:不知怎的,你得到了一个空字符串。。。看看你的数据文件。。。还要注意的是,仅仅执行
int(countries[i][0])
不会改变词典中的任何内容。int
也不适用。你必须做x=int(x)
@Blender——我只是把它添加到我的评论中:)那么在这个较低的for循环中有没有一种方法可以让我改变它呢?我可以做一些类似(国家[I])[0]=int((国家[I])[0])
?