Edit text file to add row numbers 
October 2nd, 2008, 01:47 PM
| | Newbie | | Join Date: Oct 2008
Posts: 4
| | Edit text file to add row numbers
Hi guys!
I just arrived here on this forum, and I hope you people can help me a bit with Python.
What I want is to edit a few large text files, by adding in front of each row the concerning rownumber. What I find out myself (not much..) is:
infile = open('infile.txt')
outfile = open('outfile.txt','w')
for i in range(len(infile)):
infile[i] = str(i) + ' ' + cb[i]
infile.close()
outfile.close()
Am I close :) ? Thanks for helping!
| 
October 2nd, 2008, 04:28 PM
| | Newbie | | Join Date: Oct 2008
Posts: 4
| |
I now have this, but it is still not working..
file = open("infile.txt")
intext = file.read()
file.close()
for i in range(len(intext)):
outtext[i] = str(i) + ' ' + intext[i]
file = open("outfile.txt","w")
file.write(outtext)
file.close()
| 
October 2nd, 2008, 05:48 PM
| | Newbie | | Join Date: Sep 2008
Posts: 18
| |
Hmmm... look into <open file>.readlines() and similarly <open file>.writelines()
-freddukes
| 
October 2nd, 2008, 06:01 PM
| | Newbie | | Join Date: Oct 2008
Posts: 4
| | Quote: |
Originally Posted by freddukes Hmmm... look into <open file>.readlines() and similarly <open file>.writelines()
-freddukes | Their is still something going wrong. This is the code I have now: - infile=open('infile.txt', 'r')
-
lines=infile.readlines()
-
infile.close()
-
inlist=list(lines)
-
outtext = {}
-
for i in range(len(inlist)):
-
outtext[i] = str(i) + ' ' + inlist[i]
-
outfile = open("outfile.txt","w")
-
outfile.writelines(str(outtext))
-
outfile.close()
If this is the input:
a
b
c
I want to have this:
0 a
1 b
2 c
What I receive with the code above is:
{0: '0 a\n', 1: '1 b\n', 2: '2 c\n'}
Who can give me the sollution? Thanks!
| 
October 2nd, 2008, 07:27 PM
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,439
| |
Given f, an open file object or list of lines from a file returned by file method readlines(), this one-liner will create a list of lines suitable for output to a file. - output = ['%d %s' % (i, line) for i, line in enumerate(f)]
Hint: Use the string method join() to write the modified data to disk.
| 
October 6th, 2008, 12:45 PM
| | Newbie | | Join Date: Oct 2008
Posts: 4
| |
It works! Thanks everybody!
This is the final code: - infile=open('2_2000.txt', 'r')
-
lines=infile.readlines()
-
infile.close()
-
outtext = ['%d %s' % (i, line) for i, line in enumerate(lines)]
-
outfile = open("3_2000.txt","w")
-
outfile.writelines(str("".join(outtext)))
-
outfile.close()
| 
October 6th, 2008, 01:21 PM
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,439
| | Quote: |
Originally Posted by Boeileh It works! Thanks everybody!
This is the final code: - infile=open('2_2000.txt', 'r')
-
lines=infile.readlines()
-
infile.close()
-
outtext = ['%d %s' % (i, line) for i, line in enumerate(lines)]
-
outfile = open("3_2000.txt","w")
-
outfile.writelines(str("".join(outtext)))
-
outfile.close()
| You can eliminate str() in the next to last line. Since join() returns one string, you can use f.write() or eliminate join(). - outfile.write("".join(outtext))
OR - outfile.writelines(outtext)
|  | | Thread Tools | Search this Thread | | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|