count = 0
for x in open(filename):
# strip removes all whitespace on the right (so the newline in this case)
# split will break a string in two based on the passed parameter
xi, yi = x.rstrip().split("\t") # multiple values can be assigned at once
count += 1
return count
>>> with open('blah.txt') as f:
... for i,xi,yi in ([i]+map(int,p.split()) for i,p in enumerate(f)):
... print i,xi,yi
...
0 1 13249
1 2 3249
2 3 43254
不需要重新发明轮子。。在
注意int('23\n')=23
这一点更清楚: 请注意,enumerate提供了一个包含计数器的生成器。在
^{pr2}$相关问题 更多 >
编程相关推荐