Python program prints an extra empty line when reading a text file

user2859603

I'm using python 3.3. I have a text file with three lines of text, as an example. I want to select a number and it will display the contents of that line number. For some reason, it prints the line I want, and prints an empty line below it.

The text file looks like this:

AAPL,Apple,700
P,Pandora,32
MW,Men's Warehouse,54.32

The output in the interpreter I get if i is 2:

>>
P,Pandora,32

>>

And the code is here:

line_number = int(input('Enter the line number: '))
with open('C:/Python33/myprogramtests/filewrite1.txt') as f:
    i = 1
    for line in f:
        if i == line_number:
            break
        i += 1
    print (line)

I did try a comma after print (line) but it didn't work. I'm guessing I'm missing some bit of code that would print just the line and not an extra whitespace line.

Jon Clements

You should provide an end='' to print to suppress the automatic behaviour of adding the equivalent of \n to output.

I'd also remove the counting logic and use islice to extract the line you wish, eg:

from itertools import islice

line_number = int(input('Enter the line number: '))
with open('yourfile') as fin:
    print(next(islice(fin, line_number - 1, line_number), ''), end='')

If you wanted to use the count approach, then you can use enumerate starting at 1, eg:

for idx, line in enumerate(fin, start=1):
    if idx == line_number:
        print(line, end='')
        break

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python program prints one blank line after reading a text file

A program that prints text into a text file

Reading the last line of an empty file on python

how to add a virtual extra line to a text file while reading it with a for loop

Why does my Python code print the extra characters "" when reading from a text file?

Python3 textcoding issue: extra first character when reading from text file using for loop

Print an empty line every other line when reading from file

Getting an Extra Empty line when exporting Excel Range to .txt file

Python prints text in next line

When reading a column in Excel file, this program reads data and empty columns

Program that prints specific characters from a line in a file

Why C program prints blank line to file?

Python open file statement is reading line from empty file

Python program to delete a specific line in a text file

Removing empty line when reading docx file in php

Reading text file line by line in Python and storing into struct as coordinates for graphing

Python: multi-line text file reading as single line

Python - Reading line by line from file to Tkinter Text

Segmentation Error in C program when reading text file

Reading a whole line containing a text string from a text file in Python

Reading csv file with extra line in pandas

Reading a large text file in python kills my program

Name not defined error python when reading file line by line

Automatically adding an incrementing number when reading text file on specific line

IndexError when reading the first word in a line of a text file

JTextPane is not adding a new line when reading text from HTML file

Writing to a file when python prints

Program that verifies if a word is in a file and prints the number of the line and the line itself

error reading text file; lines read twice and with every other having extra line break