blank lines in file after sorting content of a tex

2020-02-26 07:37发布

I have this small script that sorts the content of a text file

# The built-in function `open` opens a file and returns a file object.

# Read mode opens a file for reading only.
try:
    f = open("tracks.txt", "r")


    try:
        # Read the entire contents of a file at once.
       # string = f.read() 
        # OR read one line at a time.
        #line = f.readline()
        # OR read all the lines into a list.
        lines = f.readlines()
        lines.sort()
        f.close()
        f = open('tracks.txt', 'w')
        f.writelines(lines) # Write a sequence of strings to a file
    finally:
        f.close()
except IOError:
    pass

the only problem is that the text is displayed at the bottom of the text file everytime it's sortened...

I assume it also sorts the blank lines...anybody knows why?

and maybe can you suggest some tips on how to avoid this happening?

thanks in advance

标签: python
3条回答
放荡不羁爱自由
2楼-- · 2020-02-26 08:15

This is a perfect opportunity to do some test-based development (see below). Some observations:

  1. In the example below, I omit the aspect of reading from and writing to a file. That's not essential to this question, in my opinion.

  2. I assume you want to strip trailing newlines and omit blank lines. If not, you'll need to adjust. (But you'll have the framework for asserting/confirming the expected behavior.)

  3. I agree with chryss above that you generally don't need to reflexively wrap things in try blocks in Python. That's an anti-pattern that comes from Java (which forces it), I believe.

Anyway, here's the test:

import unittest

def sort_lines(text):
    """Return text sorted by line, remove empty lines and strip trailing whitespace."""
    lines = text.split('\n')
    non_empty = [line.rstrip() for line in lines if line.strip()]
    non_empty.sort()
    return '\n'.join(non_empty)

class SortTest(unittest.TestCase):

  def test(self):
    data_to_sort = """z some stuff
c some other stuff


d more stuff after blank lines
b another line
a the last line"""

    actual = sort_lines(data_to_sort)
    expected = """a the last line
b another line
c some other stuff
d more stuff after blank lines
z some stuff"""

    self.assertEquals(actual, expected, "no match!")

unittest.main()
查看更多
劳资没心,怎么记你
3楼-- · 2020-02-26 08:18

An "empty" line read from a text file is represented in Python by a string containing only a newline ("\n"). You may also want to avoid lines whose "data" consists only of spaces, tabs, etc ("whitespace"). The str.strip() method lets you detect both cases (a newline is whitespace).

f = open("tracks.txt", "r")
# omit empty lines and lines containing only whitespace
lines = [line for line in f if line.strip()]
f.close()
lines.sort()
# now write the output file
查看更多
Lonely孤独者°
4楼-- · 2020-02-26 08:27

The reason it sorts the blank lines is that they are there. A blank line is an empty string followed by \n (or \r\n or \r, depending on the OS). Perfectly sortable.

I should like to note that "try:" nested into a "try:... except" block is a bit ugly, and I'd close the file after reading, for style's sake.

查看更多
登录 后发表回答