blank lines in file after sorting content of a text file in python

后端 未结 3 1201
北恋
北恋 2020-12-31 11:41

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 fi         


        
3条回答
  •  感情败类
    2020-12-31 11:50

    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()
    

提交回复
热议问题