Search code examples
pythonstringimportfrequencypython-re

in python , need to save all results, not last result only


this is working code for counting words in file but the problem is (result.csv) contain only last result, not all results.

what should be the code look like after fixing ?

Thanks

import re
import string

frequency = {}


out_filename = "result.csv"
headers = "word,requency \n"

document_text = open('joined.xml', 'r')
text_string = document_text.read().lower()
match_pattern = re.findall(r'\b[a-z]{3,15}\b', text_string)


for word in match_pattern:
    count = frequency.get(word,0)
    frequency[word] = count + 1

frequency_list = frequency.keys()

for words in frequency_list:
    print(words, frequency[words])

with open(out_filename, "w") as fw:
  fw.write(headers)
  fw.write(words + ", " + str(frequency[words]) + "\n")

Solution

  • You should iterate over all the word-frequency pairs and write each to a separate line.

    with open(out_filename, "w") as fw:
      fw.write(headers)
      for word, freq in frequency.items():
          fw.write(word + ", " + str(freq) + "\n")