Bottom Page

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.
Quote
#2
I guess you have 25 blank lines at the end of your input file 'unsorted_fruits.txt'
Quote
#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")
Quote
#4
No there are no lines on the input file.
Quote
#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.
raven61 likes this post
Quote
#6

.txt   unsorted_fruits.txt (Size: 259 bytes / Downloads: 27)
here is the unsorted_fruits txt file.
Quote
#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']
buran likes this post
Quote
#8
Thank you everybody for your help!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Difference in list output OokaydO 6 253 Nov-09-2019, 12:33 AM
Last Post: OokaydO
  blank space + input() ClassicalSoul 6 490 Mar-06-2019, 09:50 AM
Last Post: perfringo
  re.finditer issue, output is blank anna 1 296 Feb-07-2019, 10:41 AM
Last Post: stranac
  os.popen output to a list .. evilcode1 1 788 Oct-02-2018, 08:42 PM
Last Post: ODIS
  storing lines from stdin in a list bghosh 2 583 May-02-2018, 01:12 PM
Last Post: gruntfutuk
  Pygsheets Keeps inserting a blank row, Need Help HiImNew 1 950 Jan-26-2018, 08:48 AM
Last Post: buran
  How to list objects on separate lines? Intelligent_Agent0 3 970 Jan-10-2018, 05:35 AM
Last Post: Intelligent_Agent0
  printing a list of lines Skaperen 7 1,357 Jul-30-2017, 03:28 AM
Last Post: Skaperen
  Print list items on separate lines with additional text DBS 2 2,324 Jan-11-2017, 02:57 AM
Last Post: DBS

Forum Jump:


Users browsing this thread: 1 Guest(s)