473,326 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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!
Oct 2 '08 #1
6 14356
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()
Oct 2 '08 #2
Hmmm... look into <open file>.readlines() and similarly <open file>.writelines()

-freddukes
Oct 2 '08 #3
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!
Oct 2 '08 #4
bvdet
2,851 Expert Mod 2GB
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.
Oct 2 '08 #5
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()
Oct 6 '08 #6
bvdet
2,851 Expert Mod 2GB
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)
Oct 6 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: matt | last post by:
I am using a text file as a database, each field delimited by || I want to be able to print all lines to a page, then by selecting one (with, say, a radio button) it will load into the form at the...
25
by: dixie | last post by:
I have some code that adds new records into a table for each ID in a list box when a button on a form is clicked. This works fine. My problem now is that I wish to be able to edit all the records...
0
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format....
0
by: Alex | last post by:
Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Configuration Public Class Main : Inherits Page ...
4
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have another page that lets users modify the shared xml...
8
by: John Salerno | last post by:
I figured my first step is to install the win32 extension, which I did, but I can't seem to find any documentation for it. A couple of the links on Mark Hammond's site don't seem to work. ...
3
by: zbenta | last post by:
Hi you guys. I need some help with a program I've written. I can not seam to find the reason for the error that ocurrs. But let me explain. I have a txt file that works as a database to store...
6
ironmonkey69
by: ironmonkey69 | last post by:
Can anyone help me write a perl script that edits a text file? The test file has three line of text and twelve columns of numbers. The ranges of numbers are 0-2500. the first number in all of the...
1
by: ollielaroo | last post by:
Hi guys, Firstly I did do a search for this one first but I couldn't find anything related in this forum. I am using Dreamweaver MX and trying to build admin pages for an ASP site. My problem is...
1
by: Xicon | last post by:
I am looking to create a program that is able to edit a text file that is not located within the program. This particular text file is always in the exact same location and is always named the exact...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.