1

I need to write the line numbers to an already existing text file in python 3. They have asked for the first 5 columns of the text file to contain a 4 digit line number followed by a space.

I've tried to do this by creating a for loop that reads the lines and adds a number (1) to the beginning of the line and then increments the number for the next line but is hasn't been working. It keeps coming up with an error saying that I cant join a string and an integer in my output.

What would the simplest way to do this be?

numberedfile = open("test.txt", "r")

numberedlist = numberedfile.readline()

for line in numberedlist:
    x = 1
    nrline = (x + line)
    x += 1
    print(nrline)
    numberedlist = numberedfile.readline()


numberedfile.close()
Emj
  • 23
  • 1
  • 1
  • 6
  • Tee issue is that you can't concatenate a string an an integer. Look at this question for some guidance: http://stackoverflow.com/questions/2847386/python-string-and-integer-concatenation, that should get you pointed in the right direction. – Ben Black May 05 '14 at 16:54
  • that makes sense. I'm just struggling to get it to iterate now. ive changed it to be: nrline = str(x) + line but the iteration doesnt work – Emj May 05 '14 at 17:03
  • I've moved the x out of the for loop which allows it to iterate. Thanks for your help :) – Emj May 05 '14 at 17:22

3 Answers3

3

This prints to stdout the text pattern you describe:

with open('/etc/passwd') as fp:
    for i, line in enumerate(fp):
        sys.stdout.write('%04d %s'%(i, line))

If you need to edit the file in place, or support multiple files, try using fileinput:

#!/usr/bin/python

import fileinput
import sys

for line in fileinput.input(inplace=True):
    sys.stdout.write('%04d %s'%(fileinput.filelineno(), line))
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • This prints perfectly except the numbers start at 0000 and not 0001. how would i change it to start at 0001? – Emj May 05 '14 at 16:48
  • @Emj change i to i+1 on the third line of text. or in the second example append +1 to fileinput.filelineno() to look like fileinput.filelineno()+1 – Jeff Langemeier May 05 '14 at 16:50
  • 1
    I'd rather suggest to use [`enumerate(iterable, start=1)`](https://docs.python.org/2/library/functions.html#enumerate). – Lukas Graf May 05 '14 at 16:51
  • let's throw in a end='' so it doesn't double space print('%04d %s'%(f.filelineno(), line),end='') – crownedzero May 05 '14 at 17:06
1
def text_lines(file):
    c = open(file, "r")
    list_of_lines = c.readlines()
    c.close()
    number = 1
    numbered_list_of_lines = []
    for i in list_of_lines:
        numbered_lines = "{0:0>4}".format(number) + " " + i
        numbered_list_of_lines.append(numbered_lines)
        number += 1
    f = open("numerated_text.txt", "w")
    for i in numbered_list_of_lines:
        f.write(i)
    f.close()
jidicula
  • 3,454
  • 1
  • 17
  • 38
Alex
  • 11
  • 1
-1
#!/usr/bin/python
numberedfile = open("test.txt", "r")
numberedlist = numberedfile.readline()
i = 0
for lines in numberdlist:
    i = i+1
    print str(i) + '\t' + lines
numberdfile.close()
Rahul
  • 1
  • 1
  • 4