0

将字典内元组中的两个元素转换为 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: ''

任何想法如何解决这个问题或更好的方法来做到这一点:

4

0 回答 0