Connecting Tech Pros Worldwide Forums | Help | Site Map

Edit text file to add row numbers

Newbie
 
Join Date: Oct 2008
Posts: 4
#1: Oct 2 '08
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!

Newbie
 
Join Date: Oct 2008
Posts: 4
#2: Oct 2 '08

re: Edit text file to add row numbers


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()
Newbie
 
Join Date: Sep 2008
Posts: 18
#3: Oct 2 '08

re: Edit text file to add row numbers


Hmmm... look into <open file>.readlines() and similarly <open file>.writelines()

-freddukes
Newbie
 
Join Date: Oct 2008
Posts: 4
#4: Oct 2 '08

re: Edit text file to add row numbers


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:
Expand|Select|Wrap|Line Numbers
  1. infile=open('infile.txt', 'r')
  2. lines=infile.readlines()
  3. infile.close()
  4. inlist=list(lines)
  5. outtext = {}
  6. for i in range(len(inlist)):
  7.     outtext[i] = str(i) + ' ' + inlist[i]
  8. outfile = open("outfile.txt","w")
  9. outfile.writelines(str(outtext))
  10. 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!
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#5: Oct 2 '08

re: Edit text file to add row numbers


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.
Expand|Select|Wrap|Line Numbers
  1. output = ['%d %s' % (i, line) for i, line in enumerate(f)]
Hint: Use the string method join() to write the modified data to disk.
Newbie
 
Join Date: Oct 2008
Posts: 4
#6: Oct 6 '08

re: Edit text file to add row numbers


It works! Thanks everybody!

This is the final code:
Expand|Select|Wrap|Line Numbers
  1. infile=open('2_2000.txt', 'r')
  2. lines=infile.readlines()
  3. infile.close()
  4. outtext = ['%d %s' % (i, line) for i, line in enumerate(lines)]
  5. outfile = open("3_2000.txt","w")
  6. outfile.writelines(str("".join(outtext)))
  7. outfile.close()
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#7: Oct 6 '08

re: Edit text file to add row numbers


Quote:

Originally Posted by Boeileh

It works! Thanks everybody!

This is the final code:

Expand|Select|Wrap|Line Numbers
  1. infile=open('2_2000.txt', 'r')
  2. lines=infile.readlines()
  3. infile.close()
  4. outtext = ['%d %s' % (i, line) for i, line in enumerate(lines)]
  5. outfile = open("3_2000.txt","w")
  6. outfile.writelines(str("".join(outtext)))
  7. 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().
Expand|Select|Wrap|Line Numbers
  1. outfile.write("".join(outtext))
OR
Expand|Select|Wrap|Line Numbers
  1. outfile.writelines(outtext)
Reply