Connecting Tech Pros Worldwide Help | Site Map

Edit text file to add row numbers

  #1  
Old October 2nd, 2008, 02:47 PM
Newbie
 
Join Date: Oct 2008
Posts: 4
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!
  #2  
Old October 2nd, 2008, 05:28 PM
Newbie
 
Join Date: Oct 2008
Posts: 4

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()
  #3  
Old October 2nd, 2008, 06:48 PM
Newbie
 
Join Date: Sep 2008
Posts: 18

re: Edit text file to add row numbers


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

-freddukes
  #4  
Old October 2nd, 2008, 07:01 PM
Newbie
 
Join Date: Oct 2008
Posts: 4

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!
  #5  
Old October 2nd, 2008, 08:27 PM
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,545

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.
  #6  
Old October 6th, 2008, 01:45 PM
Newbie
 
Join Date: Oct 2008
Posts: 4

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()
  #7  
Old October 6th, 2008, 02:21 PM
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,545

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open a window in a window gchq answers 7 January 4th, 2009 05:25 AM
VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help gunimpi answers 0 January 10th, 2007 08:55 PM