Python:你如何打开一个单独的输出文件?

时间:2016-03-29 01:49:32

标签: python output alphabetical

以下代码列出了文本文件中每个单词的出现次数。现在我想打开一个类似的输出文件,但是在单独的输出文件中按字母顺序排列。我的第一步是什么?

print("What's the name of the file?")
    file_name = input()
    file = open(file_name, 'r')
    wordcount={}
    for word in file.read().split():
        if word not in wordcount:
            wordcount[word] = 1
        else:
            wordcount[word] += 1
    for a,b in wordcount.items():
        print(a, b)

3 个答案:

答案 0 :(得分:1)

您的第一步显然是按字母顺序对单词进行排序。为此,您可以使用内置排序方法的pythons。首先,使用words=wordcount.keys()获取字典中的键列表,这是您的单词列表,然后您可以使用words.sort()之类的字母顺序对该单词列表进行排序。

答案 1 :(得分:1)

txt='Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
wordcount={}
for word in txt.split():
    if word not in wordcount:
        wordcount[word] = 1
    else:
        wordcount[word] += 1

list1=[]        
for a,b in wordcount.items():
    # print(a, b)
    list1.append([  a.lower(),b])

print sorted(list1, key=lambda x: x[0])    

输出:

[['ad', 1], ['adipiscing', 1], ['aliqua.', 1], ['aliquip', 1], ['amet,', 1], ['anim', 1], ['aute', 1], ['cillum', 1], ['commodo', 1], ['consectetur', 1], ['consequat.', 1], ['culpa', 1], ['cupidatat', 1], ['deserunt', 1], ['do', 1], ['dolor', 2], ['dolore', 2], ['duis', 1], ['ea', 1], ['eiusmod', 1], ['elit,', 1], ['enim', 1], ['esse', 1], ['est', 1], ['et', 1], ['eu', 1], ['ex', 1], ['excepteur', 1], ['exercitation', 1], ['fugiat', 1], ['id', 1], ['in', 3], ['incididunt', 1], ['ipsum', 1], ['irure', 1], ['labore', 1], ['laboris', 1], ['laborum.', 1], ['lorem', 1], ['magna', 1], ['minim', 1], ['mollit', 1], ['nisi', 1], ['non', 1], ['nostrud', 1], ['nulla', 1], ['occaecat', 1], ['officia', 1], ['pariatur.', 1], ['proident,', 1], ['qui', 1], ['quis', 1], ['reprehenderit', 1], ['sed', 1], ['sint', 1], ['sit', 1], ['sunt', 1], ['tempor', 1], ['ullamco', 1], ['ut', 1], ['ut', 2], ['velit', 1], ['veniam,', 1], ['voluptate', 1]]

答案 2 :(得分:0)

如何尝试使用collections并执行类似的操作来根据键对字典进行排序。

import collections
wordcount = collections.OrderedDict(sorted(wordcount.items()))

然后你可以循环遍历每一个并将其写入文件。

outPath = "PathToOutFile"

outFile = open(outPath,"w");
for a,b in wordcount.items():
     outFile.write("{:s} {:d}\n".format(a,b))