infile=open('resultado.txt','r')
lines=infile.readlines()
infile.close()
outtext=['%d %s' % (i+1,line) for i, line in enumerate(lines)]
outfile = open ('res.txt','w')
outfile.write(str(''.join(outtext)))
outfile.close()
Edit text file to add row numbers (begins in 1 ) - Python
If this is the input:
a
b
c
I want to have this:
1 a
2 b
3 c
I have this code:
Code:
infile=open('resultado.txt','r')
lines=infile.readlines()
infile.close()
outtext=['%d %s' % (i,line) for i, line in enumerate(lines)]
outfile = open ('res.txt','w')
outfile.write(str(''.join(outtext)))
outfile.close()
Leave a comment: