files = ['text1.txt', 'text2.txt']
for (id, file) in enumerate(files, start=1):
with open(file, 'r') as f:
for line in f.xreadlines():
print line.strip() + str(id)
>>> for i, f in enumerate([open('text1.txt'), open('text2.txt')], start=1):
... for line in f:
... print '%s%s' % (line.strip(), i)
... f.close()
...
sam1
mat1
tom1
robbie2
peter2
steve2
试试这个:
相关问题 更多 >
编程相关推荐