0

How can I print each individual element of a list on separate lines, with the line number of the element printed before the element? The list information will also be retrieved from a text file.

So far I have,

import sys
with open(sys.argv[1], 'rt') as num:
    t = num.readlines()
    print("\n"[:-1].join(t))

Which currently gives the read-out:

Blah, blah, blah
etc, etc, etc
x, x, x

But I want this read-out:

1. Blah, blah, blah
2. etc, etc, etc
3. x, x, x

Thanks for you help.

user1825241
  • 846
  • 4
  • 9
  • 17
  • 1
    Why not just `"".join(t)`? -- "\n"[:-1].join(t)` is pretty difficult to read... – mgilson Nov 15 '12 at 18:34
  • It would be easy for you if you explicitly iterate over the lines, instead of printing them through a join expression. – Valdir Stumm Junior Nov 15 '12 at 18:37
  • I've switched it to "" from "\n" now, the [:-1] is to get rid of the \n that is automatically added to the end of each element in a list. – user1825241 Nov 15 '12 at 18:41
  • @user1825241 -- I think you're confused. You're joining a bunch of strings (which have a newline at the end) inserting an empty string in between each. That gives the same result as stripping the newline and joining with a newline, but the latter case is not what you're doing. – mgilson Nov 15 '12 at 18:43

3 Answers3

2

you can use enumerate() and use str.rstrip() to remove the all types of trailing whitespaces from the string. and you can also use rstrip('\n') if you're sure that the newline is going to be \n only.:

for i,x in enumerate(t,1):
    print ("{0}. {1}".format(i,x.rstrip()))
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • This will give you an extra newline per line. (You get one from the line and 1 from `print`.) – mgilson Nov 15 '12 at 18:40
  • Now you're stripping characters from both ends. Perhaps `.rstrip('\n')`? – mgilson Nov 15 '12 at 18:41
  • @mgilson I think then just `rstrip()` is okay, as some OS also use `\r\n` as newline. – Ashwini Chaudhary Nov 15 '12 at 18:43
  • But `rstrip` will take off all trailing whitespace -- It's usually not a problem, but it's worth considering at least. If you open the file with universal newline support, then you're set with just `.rstrip('\n')` – mgilson Nov 15 '12 at 18:47
0

This seems easiest with enumerate, printing each line as you go:

import sys
with open(sys.argv[1], 'r') as num:
    for i,line in enumerate(num,1):
        print("%d. %s"%(i,line[:-1]))  #equivalent to `"{0}. {1}".format(i,line[:-1])`

Of course, you could wrap the whole thing in a generator if you want to use join:

def number_lines(fname):
    with open(fname,'r') as f:
       for i,line in enumerate(f,1):
           yield "%d. %s"%(i,line[:-1])

print('\n'.join(number_lines(sys.argv[1])))
mgilson
  • 300,191
  • 65
  • 633
  • 696
0

Using fileinput.input it is possible to iterate over file line by line. When fileinput.input returns next line, we enumerate it to track line number. As input already contains newline we use end="" in print method so it doesn't append unnecessary newline. Using format to convert line number and content to formated representation.

import fileinput

for number, line in enumerate(fileinput.input(['some_file.txt']), 1):
    print('{0}. {1}'.format(number, line), end="")
abele
  • 437
  • 4
  • 15