Counting punctuation in text using Python and regex

python, regex, text-mining

Solution

In [1]: from string import punctuation

In [2]: from collections import Counter

In [3]: counts = Counter(open('novel.txt').read())

In [4]: punctuation_counts = {k:v for k, v in counts.iteritems() if k in punctuation}

Problem

I am trying to count the number of times punctuation characters appear in a novel. For example, I want to find the occurrences of question marks and periods along with all the other non alphanumeric characters. Then I want to insert them into a csv file. I am not sure how to do the regex because I don't have that much experience with python. Can someone help me out? ``` texts=string.punctuation counts=dict(Counter(w.lower() for w in re.findall(r"\w+", open(cwd+"/"+book).read()))) writer = csv.writer(open("author.csv", 'a')) writer.writerow([counts.get(fieldname,0) for fieldname in texts]) ```

Original source