我无法弄清楚如何查找其中包含2个或更多元音的所有单词。到目前为止,这就是我所拥有的,但是当我运行它时,它不会给我任何输出。我很感激帮助。
import re
def main():
in_f = open("jobs-061505.txt", "r")
read = in_f.read()
in_f.close()
for word in read:
re.findall(r"\b[aAeEiIoOuU]*", read)
in_f = open("twoVoweledWordList.txt", "w")
in_f.write(word)
in_f.close()
print (word)
main()
如果这不是正确的格式,我道歉。
答案 0 :(得分:0)
for word in read: <--- iterating over chars in "read"!
re.findall(r"\b[aAeEiIoOuU]*", read) <-- using read again, discarding result
您的迭代和模式使用不对齐。另外,你不能使用结果。
考虑逐行处理文件等。
twovowels=re.compile(r".*[aeiou].*[aeiou].*", re.I)
nonword=re.compile(r"\W+", re.U)
file = open("filename")
for line in file:
for word in nonword.split(line):
if twovowels.match(word): print word
file.close()
答案 1 :(得分:0)
使用re.findall功能查找包含至少两个元音的所有单词
>>> s = """foo bar hghghg ljklj jfjgf o jgjh aei
bar oum"""
>>> re.findall(r'\S*?[aAeEiIoOuU]\S*?[aAeEiIoOuU]\S*', s)
['foo', 'aei', 'oum']
>>> re.findall(r'\w*?[aAeEiIoOuU]\w*?[aAeEiIoOuU]\w*', s)
['foo', 'aei', 'oum']
答案 2 :(得分:0)
a='hello how are you"
[ x for x in a.split(' ') if len(re.findall('[aeiouAEIOU]',x))>=2 ]
代码中的修改
import re
def main():
in_f = open("jobs-061505.txt", "r")
read = in_f.read()
words = [ x for x in re.findall('\w+',read) if len(re.finall('[aeiouAEIOU]',x))>=2 ]
print words
int上面的代码'read()
将整个文件读为字符串。 re.findall('\ w +',read)会给你一个单词列表。如果列表长度大于或等于2。它将被存储为列表。
现在你可以对输出做任何事了。
答案 3 :(得分:-1)
我建议使用此命令:
re.findall('\S*[aAeEiIoOuUyY]\S*[aAeEiIoOuUyY]\S*', str)
str 是您在其中查找包含两个或更多元音的单词的字符串。
REGEX解释:
\ S - 表示每个非白色字符&#39;
[aAeEiIoOuUyY] - 它代表括号中的每个字符(所以&#39; a&#39; OR&#39; A&#39; OR&#39; e&#39;等)
a * - 这意味着在*之前可以出现任意数量的字符(&#39; a&#39;在这种情况下)
示例:强>
字符串:
str = "aaa bbb abb koo llk tr"
Python代码:
import re
re.findall('\S*[aAeEiIoOuUyY]\S*[aAeEiIoOuUyY]\S*', str)
输出:
['aaa', 'koo']