1

I worked on a code that is counting the occurrencies of words in a given text document and now I would like to save the output on a csv file with two columns: one for the words and one for their frequencies.

This is the code that I am trying to replicate:

from collections import Counter
counter = Counter(['spam', 'egg', 'spam', 'egg', 'python', 'egg'])

with open('wordfile.csv', 'w') as f:
    writer = csv.writer(f, delimiter=' ')
    writer.writerow(('word', 'count'))
    writer.writerows(counter.most_common())     

However, this is the output:

word countegg 3spam 2python 1

The output that I am trying to get should contain just two columns (one for the "word" adn one for the "frequency" and each row should contain a word and its occurrance in the text:

word, frequency,
the, 3165,
in, 1265,
of,1233,

When I print the output with the code:

print(open('wordfile.csv', 'rb').read())

I get:

b'word count\r\r\negg 3\r\r\nspam 2\r\r\npython 1\r\r\n')

As you can see there aren't the two columns for the word and the frequency. I am using Windows and this is the Python version that I am using: 3.5.2 |Anaconda 4.1.1 (64-bit)

1
  • For writing CSV, it is often easier to simply write the values using file.write(). Commented Nov 11, 2016 at 16:24

1 Answer 1

2

writer.writerow is expecting the list of columns for the row; in you snippet your are passing all the results ie. all the columns of all the items.

You need to iter over counter.items() to get the result you want:

import csv
from collections import Counter

counter = Counter(['spam', 'egg', 'spam', 'egg', 'python', 'egg'])

with open('wordfile.csv', 'w', newline='') as f:
    writer = csv.writer(f, delimiter=' ')
    writer.writerow(('word', 'count'))
    writer.writerows(counter.most_common())
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks indeed you can also use writerows.
by using counter.items() I get an error: 'list' object has no attribute 'items'
@M.Huntz i fixed the code to use .most_common() and writer.writerows with an s that's what you were missing.
@M.Huntz: to get comma as a separator, drop delimiter=' ' parameter (comma is the default).
This is the output by using the code word,count,egg,3spam,2python,1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.