int()的基数为10的无效文字:'16:00:00'

时间:2014-02-06 07:58:42

标签: python-2.7

dic = dict()
with open('C:\\Users\\aman\\Documents\\dataVal.txt', 'r') as fh:
    for l in fh.readlines():
        try:
            lines = l.split()
            date, sub, num = lines[0], lines[1], [int(x) for x in lines[2:]]
            dic.setdefault(date, {})
            dic[date][sub] = num
        except Exception as er:
            print er
print dic

有人可以帮忙吗? 它给我一个错误说int()的无效文字与基数10:'16:00:00'。如何摆脱它?仅供参考'16:00:00'是txt文件表格中的第一列

16:00:00     Maths   100  95  65  32  23  45  77  54  78  88  45  67  89
17:00:00    Science 45   53  76  78  54  78  34  99  55  100 45  56 78
18:00:00  English 43   45  56  76  98  34  65  34  45  67  76  34  98

1 个答案:

答案 0 :(得分:1)

我将int(x)改为str(x),请你试试。如果那是错误。

dic = dict()
with open('C:\\Users\\aman\\Documents\\dataVal.txt', 'r') as fh:
    for l in fh.readlines():
        try:
            lines = l.split()
            date, sub, num = lines[0], lines[1], [str(x) for x in lines[2:]]
            dic.setdefault(date, {})
            dic[date][sub] = num
        except Exception as er:
            print er
print dic

输出:

 {'17:00:00': {'Science': ['45', '53', '76', '78', '54', '78', '34', '99', '55', '100', '45', '56', '78']}, 
  '18:00:00': {'English': ['43', '45', '56', '76', '98', '34', '65', '34', '45', '67', '76', '34', '98']}, 
  '16:00:00': {'Maths': ['100', '95', '65', '32', '23', '45', '77', '54', '78', '88', '45', '67', '89']}}