-4

我有一个包含以下功能的模块文件:

def replace(filename):
    match = re.sub(r'[^\s^\w]risk', 'risk', filename)
    return match

def count_words(newstring):
    from collections import defaultdict
    word_dict=defaultdict(int)
    for line in newstring:
        words=line.lower().split()
        for word in words:
            word_dict[word]+=1

    for word in word_dict:
        if'risk'==word:
           return word, word_dict[word] 

当我在 IDLE 中执行此操作时:

>>> mylist = open('C:\\Users\\ahn_133\\Desktop\\Python Project\\test10.txt').read()
>>> newstrings=replace(mylist)    
>>> newone=count_words(newstrings)

test10.txt 仅包含用于测试的单词,例如:

#

风险风险更大的风险。风险?

#

我收到以下错误:

Traceback (most recent call last):
  File "<pyshell#134>", line 1, in <module>
    newPH = replace(newPassage)
  File "C:\Users\ahn_133\Desktop\Python Project\text_modules.py", line 56, in replace
    match = re.sub(r'[^\s^\w]risk', 'risk', filename)
  File "C:\Python27\lib\re.py", line 151, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or buffer

无论如何运行这两个函数而不保存newstrings到文件中,使用打开它readlines(),然后运行count_words函数?

4

1 回答 1

0

要运行模块,您只需执行python modulename.pypython.exe modulename.py- 或双击图标。

但我想你的问题真的不是你的问题标题所说的,所以你真的应该学习如何调试 python

于 2012-09-01T05:56:07.717 回答