追溯表明文件末尾可能有一个空行。您可以这样解决:
f = open('test.txt','r')g = open('test1.txt','w') while True: x = f.readline() x = x.rstrip() if not x: break print >> g, int(x, 16)另一方面,最好使用
for x in f代替
readline。不要忘记关闭文件,或者更好地使用
with该文件来关闭它们:
with open('test.txt','r') as f: with open('test1.txt','w') as g: for x in f: x = x.rstrip() if not x: continue print >> g, int(x, 16)


