在Python中对文本文件内容排序后出现空行

11 投票
3 回答
33556 浏览
提问于 2025-04-15 23:43

我有一个小脚本,可以对一个文本文件的内容进行排序。

# 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

不过唯一的问题是,每次排序后,文本的内容都会显示在文本文件的底部……

我猜它也会把空行一起排序……有没有人知道这是为什么呢?

还有,能不能给我一些建议,告诉我怎么避免这种情况发生?

提前谢谢大家!

3 个回答

4

之所以会对空行进行排序,是因为空行确实存在。空行就是一个空字符串后面跟着换行符(根据操作系统的不同,可能是\n、\r\n或者\r)。所以它们是可以被排序的。

我想提一下,把“try:”放在“try:... except”块里面看起来有点别扭,而且我建议在读取完文件后关闭它,这样更符合代码风格。

6

这是一个进行基于测试开发的好机会(见下文)。以下是一些观察:

  1. 在下面的例子中,我省略了从文件读取和写入的部分。我认为这对这个问题不是关键。

  2. 我假设你想去掉结尾的换行符,并且不想要空行。如果不是这样,你需要做一些调整。(不过你会有一个框架来验证预期的行为。)

  3. 我同意上面提到的观点,在Python中通常不需要强行把代码放在try块里。这是一种不好的习惯,源于Java(因为Java强制这样做),我认为。

总之,这里是测试代码:

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

在Python中,从文本文件读取的“空”行是用一个只包含换行符("\n")的字符串来表示的。你可能还想避免那些“数据”仅由空格、制表符等(也就是“空白”)组成的行。使用str.strip()这个方法,你可以同时检测到这两种情况(换行符也是空白的一种)。

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

撰写回答