Cha*_*eow 2 python regex text-mining
我试图计算标点符号出现在小说中的次数.例如,我想找到问号和句号的出现以及所有其他非字母数字字符.然后我想将它们插入到csv文件中.我不知道怎么做正则表达式因为我没有那么多的python经验.有人可以帮我吗?
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])
Run Code Online (Sandbox Code Playgroud)
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}
Run Code Online (Sandbox Code Playgroud)