Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old October 2nd, 2008, 02: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, 05: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, 06: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, 07: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, 08:27 PM
bvdet's Avatar
Expert
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,219
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, 01: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, 02:21 PM
bvdet's Avatar
Expert
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,219
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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles