how to write add frequency in particular file by reading a csv file and then making a new file of multiple csv file by adding frequency

Mark Byrne mbyrnepr2 at gmail.com
Fri Jun 23 16:07:15 EDT 2017


A problem (possibly the problem) is the lines which use the get function:
count = frequency.get(word,0)

Since the dictionary is empty at the start of the loop, the get function is
passing a value of 0 to count and count1.
The subsequent updates to the dictionary are applying a value of zero

As a result, the file being written to contains count values of 0 for each
word.

Possible fix is to replace this:

count = frequency.get(word,0)
count1 = frequency.get(word1,0)
if word1 == word:
    frequency[word] = count + count1
else:
    frequency[word] = count

with this:

if word1 == word:
    if word in frequency:
        frequency[word] += 1
    else:
        frequency[word] = 1



More information about the Python-list mailing list