文件处理和计算文件中字符串的出现次数

时间:2017-02-04 11:22:40

标签: python python-3.x

Occurrences( inputFileNames, words, outputFileName )

对于列表inputFileNames中的每个文件,输出到 一个名为outputFileName的文件,输入名称 文件和列表words中的每个单词的数量 出现个别词;如果有任何输入 无法读取文件,发出合适的错误消息 并跳过该文件。为了增加乐趣,请不要使用 .count()内置函数。

Occurrences( ["sample1.txt","sample2.txt","sample3.txt"], ["why","you","fate","among"], "out.txt")

out.txt然后包含:

File Name: why you fate among sample1.txt 3 0 0 0 sample2.txt 2 2 1 1 sample3.txt 0 3 0 0

到目前为止我所得到的是

def Occurrences(inputFileNames,words,outputFileName):
    output = open(outputFileName,"a")

    try:
        for file in inputFileNames:
            opned = open(file,"r")
            print(opned)
            counters = [0 for file in range (len(words))]
            index = 0
            for i in words:
                for line in opned:
                    if i in line:
                        print("WORD",i,"LINE",line)
                        counters[index] += 1
                index +=1
            print(counters)

    except IOError:
        file.close()
        print("*** Occurrences: File handle Error")

1 个答案:

答案 0 :(得分:0)

我也肯定会建议使用count方法。从您的示例中,我无法确切地看到您尝试将结果写入输出文件的位置,因此我将解释可能的实现。

If we can write code in this way then no need to use precedence.

 <article> 
     <h4>HEADING</h4>
 </article>
 <aside>
     <h4>HEADING</h4>
 </aside>

aside h4
{
     font-style:italic!important;
     color:yellow;
 }
 article h4
 {
     color:black;
     font-style:normal!important;
 }
然后

occ.txt包含:

def occurrences(inputFileNames, words, outputFileName):
    wordCount = {}
    # This dictionary will hold our wordCount and be used for construnction of the output file

    for file in inputFileNames:
        # Iterate over the files
        try:
            with open(file, 'r') as infile:
                content = infile.read().strip().split(" ")
            # Declare entry to wordCount for file only if no IOError is raised
            wordCount[file] = [0 for j in range(len(words))]
            for i in  range(len(words)):
                # Instead of iterating over the contents manually, split them and use the count method
                wordCount[file][i] = str(content.count(words[i]))
        except IOError:
            print("The file {} could not be read.".format(file))

    with open(outputFileName, 'w+') as outfile:
        # Iterate over the wordCount dict and write the output
        for i in wordCount.keys():
            outfile.write(i+" "+" ".join(wordCount[i])+"\n")
occurrences(["book.txt"], ["Alice", "hole", "Rabbit"], "occ.txt")

要在没有count方法的情况下执行此操作,一种可能的方法是按元素迭代内容列表元素,并在单词与元素匹配时递增计数。

book.txt 155 0 26