Python 2.7 以10为基数的int()的文本无效:';16:00:00';
有人能帮忙吗? 它给了我一个错误,告诉我以10为基数的int()的无效文本:“16:00:00”。如何消除它?仅供参考“16:00:00”是txt文件表中的第一列Python 2.7 以10为基数的int()的文本无效:';16:00:00';,python-2.7,Python 2.7,有人能帮忙吗? 它给了我一个错误,告诉我以10为基数的int()的无效文本:“16:00:00”。如何消除它?仅供参考“16:00:00”是txt文件表中的第一列 dic = dict() with open('C:\\Users\\aman\\Documents\\dataVal.txt', 'r') as fh: for l in fh.readlines(): try: lines = l.split() date,
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(x)改为str(x),你能试试吗。如果这是错误。
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
输出:
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
请把你要分析的行和错误写出来。顺便说一句,如果您还拥有字符串类型,请将int(x)更改为str(x)。您希望如何输出时间作为键'你能粘贴你得到的错误吗?'16:00:00'可以用作字符串,所以你的代码应该在这里工作,让我们先确认你的错误。我看不出你的代码有任何错误,除了我修复的缩进。你添加的解决方案片段。它工作得很好。非常感谢。你是个天才。啊,很抱歉,我更改了
try
和的缩进,除了立即检查
{'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']}}