【发布时间】:2018-11-11 08:55:28
【问题描述】:
我想一次遍历所有行并检查字符串是否在任何行中,如果是则应用函数并跳出循环,如果不是则检查第二个字符串并执行相同操作事物。如果在任何行中都没有找到字符串,则继续使用 else..
split= text.as_string().splitlines()
for row in split:
if 'Thanks Friend' in row.any():
apply_some_function()
break
elif 'other text' in row.str.any():
apply_some_function()
break
else:
.......
我不断收到错误:
AttributeError Traceback (most recent call last)
<ipython-input-179-8f0e09f62771> in <module>()
1 for row in split:
2
----> 3 if 'Thanks Friend' in row.str.any():
4 apply_some_function()
5 break
AttributeError: 'str' object has no attribute 'str'
【问题讨论】:
-
怎么做,上面的代码不起作用,不断出现AttributeError: 'str' object has no attribute 'str'
-
好吧,我猜你是Java背景的。在 Python 中,你可以做的事情更简单。这只是:
if 'Thanks Friend' in row:和elif 'other text' in row:,如果我没记错的话。 -
我想为每个条件一次遍历所有行,而不是一次一个