我有一个包含以下功能的模块文件:
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
函数?
答案 0 :(得分:0)
要运行模块,您只需执行python modulename.py
或python.exe modulename.py
- 或者只需双击该图标。
但我想你的问题确实不是你的问题标题所说的,所以你真的应该学习how to debug python