enunerate()函数和推导式生成列表

1 a = ['I\n','love\n','you\n']
2 b = enumerate(a)
3 print(list(b))

 

 

1 with open('b.txt','r',encoding='UTF-8') as f:
2     lines = f.readlines()
3     lines = [line.rstrip()+'#'+str(index+1)+'\n' for index,line in enumerate(lines)]
4 
5 with open('e.txt','w',encoding='UTF-8') as f:
6     f.writelines(lines)