按字母顺序对大型.text文件进行排序

时间:2014-12-15 14:21:02

标签: python

我对以下Python脚本有轻微问题。

我正在尝试按字母顺序对列表进行排序(区分大小写,并且可能包含数字)。该列表是从输入文件中读取的内容生成的。

输入文件相当大,大小约为219MB。但是,在我运行脚本后,输出.txt文件只有1570KB。有人可以帮我确定为什么会这样吗?

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

# Read mode opens a file for reading only.
try:
    f = open("dict.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('output.txt', 'w')
        f.writelines(lines) # Write a sequence of strings to a file
    finally:
        f.close()
except IOError:
    pass

0 个答案:

没有答案