1

这是字数统计程序。怎么能变得更简单?

import re
from collections import Counter

with open('C:\Data\test.txt') as f:
passage = f.read()

words = re.findall(r'\w+', passage)

cap_words = [word.upper() for word in words]

word_counts = Counter(cap_words)

不断收到此错误消息:

Traceback (most recent call last):
File "C:/Python27/wordcount", line 4, in <module>
with open('C:\Data\test.txt') as f:
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Data\test.txt'
4

1 回答 1

3

使用原始字符串或\使用\. 这是必需的,因为没有它将'\t'被转换为制表符空间:

r'C:\Data\test.txt'

例子:

>>> print 'C:\Data\test.txt'
C:\Data est.txt                 #\t is converted to tab
>>> print r'C:\Data\test.txt'   
C:\Data\test.txt                #here it is fine

>>> print 'C:\\Data\\test.txt'  #same as raw string, but manual escaping
C:\Data\test.txt
于 2013-07-05T17:12:57.413 回答