Python Forum
25 blank lines in my sorted_fruits output list!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
25 blank lines in my sorted_fruits output list!
#1
25 blank lines in my sorted_fruits output list don't know how to get rid of them! Don't know how they got there!here is my script
# This just gives you a feel that something is happening.
print('Sorting started')

# This defines where the ipnput and output files are
fin = open('unsorted_fruits.txt', 'r')
fout = open('sorted_fruits.txt', 'w')

# This reads the input file
Fruits = fin.readlines()

# This sorts the input file
Fruits.sort()

# This for loop writes the fruits to the sorted_fruits list.
for fruit in Fruits:
        fout.write(fruit)

# This cloes both files.
fin.close()
fout.close()

# This just lets you know that the sorting is finished.
print('Sorting finished')
If you run it the output sorted_fruits.txt file has 25 empty lines and then the list starts. This was for home work but i've already turned it in I just want to know if I can get the lines removed? I can't figure it out.
Reply
#2
I guess you have 25 blank lines at the end of your input file 'unsorted_fruits.txt'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Does unsorted_fruits.txt come from a different os?

test the line endings

import os

padding = 20

file = '/unsorted_fruits.txt'
if os.path.isfile(file):
    if "\r\n" in open(file,"r").read():
        print (file.ljust(padding, " ")," : DOS line endings found")
    if "\n" in open(file,"r").read():
        print  (file.ljust(padding, " "), " : UNIX line endings found")
    if "\r" in open(file,"r").read():
        print (file.ljust(padding, " ")," : MAC line endings found")
Reply
#4
No there are no lines on the input file.
Reply
#5
Can you share your input file? I don't think it's line-ending issue because in this case it will be extra blank line between other lines.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6

.txt   unsorted_fruits.txt (Size: 259 bytes / Downloads: 174)
here is the unsorted_fruits txt file.
Reply
#7
Testing file.
with open('fruit.txt') as f:
    for line in f:
        print(repr(line))
Output:
'papaya\n' '\n' 'kiwifruit\n' '\n' 'zapote blanco\n' '\n' 'huckleberry\n' '\n' 'banana\n' '\n' 'fig\n' '\n'
So the maker of file have manged to get 2 new line \n between words.
Clean up:
with open('fruit.txt') as f:
    for line in f:
        line = line.strip()
        if line.strip():
            print(line)
Output:
papaya kiwifruit zapote blanco huckleberry banana fig lime xigua vanilla
Want a list:
with open('fruit.txt') as f:
    lst = [line.strip() for line in f if line.strip()]
Output:
>>> lst[:10] ['papaya', 'kiwifruit', 'zapote blanco', 'huckleberry', 'banana', 'fig', 'lime', 'xigua', 'vanilla', 'yiessas']
Reply
#8
Thank you everybody for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to insert Dashed Lines in between Rows of a tabulate output Mudassir1987 0 463 Sep-27-2023, 10:09 AM
Last Post: Mudassir1987
  Why the blank lines in output? Mark17 6 1,388 Jun-27-2022, 11:21 PM
Last Post: Mark17
  sum() list from SQLAlchemy output Personne 5 4,345 May-17-2022, 12:25 AM
Last Post: Personne
  Remove Blank Lines from docx table and paragraphs bsudhirk001 1 3,619 Feb-14-2021, 12:38 AM
Last Post: Larz60+
  How to append to list a function output? rama27 5 6,643 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,706 Aug-10-2020, 11:01 PM
Last Post: medatib531
  json.dumps list output qurr 12 5,072 Apr-08-2020, 10:13 PM
Last Post: micseydel
  how do i get rid of next lines in my list() ironpotatoe58 10 3,915 Mar-31-2020, 03:57 AM
Last Post: SheeppOSU
  If item in list = true, Output = xx kroh 0 1,456 Feb-19-2020, 09:17 AM
Last Post: kroh
  CSV gives me blank row on PC, but not a Mac bazcurtis 2 2,740 Jan-06-2020, 08:40 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020