浏览量 3889
2018/09/12 17:03
>>> my_list=['a','b','c']
>>> for idx,val in enumerate(my_list,1):
... print idx, val
...
1 a
2 b
3 c
[root@wangzi]# cat a.txt
12 3
i2 2
aai b
23 9
[root@wangzi]# cat enumerate.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
def parse_data(filename):
with open(filename,'r') as f:
for lineno, line in enumerate(f,1):
fields=line.split()
try:
count=int(fields[1])
except ValueError as e:
print('Line {}:Parse error:{}'.format(lineno,e))
parse_data(sys.argv[1])
[root@wangzi]# python enumerate.py a.txt
Line 3:Parse error:invalid literal for int() with base 10: 'b'
上一篇 搜索 下一篇