【发布时间】:2017-09-29 08:51:21
【问题描述】:
我已经阅读了多篇关于此错误的帖子,但我仍然无法弄清楚。当我尝试遍历我的函数时:
def fix_Plan(location):
letters_only = re.sub("[^a-zA-Z]", # Search for all non-letters
" ", # Replace all non-letters with spaces
location) # Column and row to search
words = letters_only.lower().split()
stops = set(stopwords.words("english"))
meaningful_words = [w for w in words if not w in stops]
return (" ".join(meaningful_words))
col_Plan = fix_Plan(train["Plan"][0])
num_responses = train["Plan"].size
clean_Plan_responses = []
for i in range(0,num_responses):
clean_Plan_responses.append(fix_Plan(train["Plan"][i]))
这是错误:
Traceback (most recent call last):
File "C:/Users/xxxxx/PycharmProjects/tronc/tronc2.py", line 48, in <module>
clean_Plan_responses.append(fix_Plan(train["Plan"][i]))
File "C:/Users/xxxxx/PycharmProjects/tronc/tronc2.py", line 22, in fix_Plan
location) # Column and row to search
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36\lib\re.py", line 191, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object
【问题讨论】:
-
如果您遇到错误,始终发布包括堆栈跟踪在内的完整错误。
-
请
print(train["Plan"][i])看看它是什么。在 for 循环中调用fix_Plan()之前执行此操作。我不认为train["Plan"][i]是你所期望的。 -
你可以添加尝试吗:除了在 fix_Plan 中,如果位置:
-
它是来自 excel 文档的字符串,格式如下: 视频编辑:使用 Wochit、Videolicious 和 iMovie 等工具进一步培养视频制作技能。制定一项针对体育运动的生产计划,将努力与潜在观众/影响相匹配。扩展 HTML/CSS 技能并确定一到两个体育项目,这些项目可以从 HTML 故事中受益,然后实施。
-
你确定它是一个字符串吗?尝试打印
type(train['Plan'][i])