基数为 10 的 int() 的无效文字:'16:00:00' [英] invalid literal for int() with base 10: '16:00:00'

查看:64
本文介绍了基数为 10 的 int() 的无效文字:'16:00:00'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 文件中表格的第一列

Can someone Help? Its giving me an error saying invalid literal for int() with base 10: '16:00:00' .How to get rid of it? Just for your information '16:00:00' is the first column in the table in the txt file

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

推荐答案

我把 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']}}

这篇关于基数为 10 的 int() 的无效文字:'16:00:00'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆