Connecting Tech Pros Worldwide Help | Site Map

Edit text file to add row numbers

 
LinkBack Thread Tools Search this Thread
  #1  
Old October 2nd, 2008, 01:47 PM
Newbie
 
Join Date: Oct 2008
Posts: 4
Default 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!
Reply
  #2  
Old October 2nd, 2008, 04:28 PM
Newbie
 
Join Date: Oct 2008
Posts: 4
Default

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

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

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

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

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

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

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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

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.