无法将字典元素(字符串)转换为int

时间:2013-02-27 02:59:20

标签: 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,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: ''

任何想法如何解决这个或更好的方法:

0 个答案:

没有答案