473,385 Members | 1,925 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,385 software developers and data experts.

Edit text file to add row numbers (begins in 1 ) - Python

2
If this is the input:
a
b
c

I want to have this:
1 a
2 b
3 c

I have this code:
Expand|Select|Wrap|Line Numbers
  1. infile=open('resultado.txt','r')
  2. lines=infile.readlines()
  3. infile.close()
  4. outtext=['%d %s' % (i,line) for i, line in enumerate(lines)]
  5. outfile = open ('res.txt','w')
  6. outfile.write(str(''.join(outtext)))
  7. outfile.close()
but my row number begins in 0
Dec 3 '12 #1
2 2659
mcff
2
Sorry it was easy, I just added i+1:
Expand|Select|Wrap|Line Numbers
  1. infile=open('resultado.txt','r')
  2. lines=infile.readlines()
  3. infile.close()
  4. outtext=['%d %s' % (i+1,line) for i, line in enumerate(lines)]
  5. outfile = open ('res.txt','w')
  6. outfile.write(str(''.join(outtext)))
  7. outfile.close()
Dec 3 '12 #2
bvdet
2,851 Expert Mod 2GB
Instead of (i,line), use (i+1,line).
Dec 3 '12 #3

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

Similar topics

6
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a...
0
by: konghou | last post by:
It's found that when exporting a table/query to a text file, numbers with decimals will be rounded off to 2 decimals (or 0 decimals sometimes). Is there an explanation or setting for that?
11
by: Nina | last post by:
I have a text file which stores user input. Later if user edited the input data from a input form. How can I update the input text file? If update the text file too often, would it cause...
4
by: Shawn | last post by:
I need to be able to open a file, search for a line of text and change the line. I am able to use the streamreader to find the line, but can seem to find a way to alter the line.
3
by: John | last post by:
How can I tell if a line begins with a number instead of a character? I need to read a text file and some of the data begins with a character which is causing me an issue in reading and uploading...
16
by: Preben Randhol | last post by:
Hi A short newbie question. I would like to extract some values from a given text file directly into python variables. Can this be done simply by either standard library or other libraries? Some...
3
by: cyberco | last post by:
I must be overlooking something here... I'm trying to edit a line in a text file. I thought this was easy with fileinput, but all examples do not write the line back to the file but simply 'print'...
16
ironmonkey69
by: ironmonkey69 | last post by:
I'm learning Python and I need some help. I have a text file with 12 columns filled with numbers. I need to write a script that would be able to select a column and zero out all the values in that...
6
by: Boeileh | last post by:
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...
0
by: Jah Mason | last post by:
/*Currently i have functionality for a user to choose a number of values to be entered into an array, add numbers into the array "numbers" and process the mean, range, variance, maximum, minimum and...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.