Reputation: 61
Salutations, I am trying to write a function that prints data from a text file line by line. The output needs to have the number of the line followed by a colon and a space. I came up with the following code;
def print_numbered_lines(filename):
"""Function to print numbered lines from a list"""
data = open(filename)
line_number = 1
for line in data:
print(str(line_number)+": "+line, end=' ')
line_number += 1
The issue is when I run this function using test text files I created, the first line is not on the same indentation level as the rest of the lines in the output, ie. the outputs look kind of like
1: 9874234,12.5,23.0,50.0
2: 7840231,70,60,85.4
3: 3845913,55.5,60.5,80.0
4: 3849511,20,60,50
Where am I going wrong? Thanks
Upvotes: 0
Views: 5252
Reputation: 5110
Replace the value of end
argument with empty string instead of space. As end
argument is a space, it's printing a space after every line. So latter lines have a space at the beginning of the line.
def print_numbered_lines(filename):
"""Function to print numbered lines from a list"""
data = open(filename)
line_number = 1
for line in data:
print(str(line_number) + ": " + line, end='')
line_number += 1
Another way you can do this, is strip
the new lines and print without passing any value to end
argument. This will remove the \n
it has at the end of the line and a new line will be printed as end="\n"
by default.
def print_numbered_lines(filename):
"""Function to print numbered lines from a list"""
data = open(filename)
line_number = 1
for line in data:
print(str(line_number) + ": " + line.strip("\n"))
line_number += 1
Upvotes: 2
Reputation: 679
You specified end
argument as a space. So after first line each has this extra space.
line that your read from file looks somehting like this:
'9874234,12.5,23.0,50.0\n'
Look at the ending. Line translation happens is due to original line.
So to get what you want you just need to change end
argument of print to empty string( not space)
Moreover, I advise you to change the implementation of the function and use enumerate
for line numbering.
def print_numbered_lines(filename):
data = open(filename)
for i, line in enumerate(data):
print(str(i+1)+": "+line, end='')
Upvotes: 0
Reputation: 239
This has to do with your print statement.
print(str(line_number)+": "+line, end=' ')
You probably saw that when printing your lines there was an extra line between them and then you tried to work around this by using end=' '
.
If you want to remove the 'empty' lines you should use line.strip()
. This removes them.
Use this:
print(str(line_number)+": "+line.strip())
strip can also take an argument. This is from the documentation:
str.strip([chars])
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:
Whats up with that?
The lines in your file are not separated into different lines by nothing. On linux a newline is represented by \n
. Normal editors convert these by pushing the text down into a new line.
When reading a file Python separates lines on exactly these \n
but doesn't throw them away. When printing they will be interpreted again and combined with the newline a print
adds there will be one newline 'too much'.
The end
parameter in your print statement simply changes what print
will use after printing a line. Default is \n
.
Check what it does when you use end=" !"
:
1: aaa
!2: bbb
!3: ccc
You can see the \n
after 'aaa' causing a newline (which is part of the string) and after that print adds the contents of end
. So it adds a !
. The next line is printed in the same line because there is no other newline that would cause a line break before printing it.
Upvotes: 0