python - 如何遍历文件的每一行并打印出任何包含两个相邻元音的单词?
问题描述
这是我尝试过的。但我收到一条错误消息:
错误:<re.Match 对象;跨度=(74, 76),匹配='ai'>
程序应该打印出任何包含两个连续元音的单词。Text.txt 文件内容:
text = "This is a test file with a single word per line. Print any words that contain two vowels next to each other." a = text.split(" ").rstrip("\n")
my_file = open("test.txt", "w")
Python代码:
reg = r"[aeiou][aeiou]"
with open("text.txt") as f:
for word in f:
word = word.strip()
print(re.search(reg, word, re.I))
解决方案
你可以这样经历:
import re
with open('test.txt') as f:
for line in f:
line = line.strip()
if re.search(r"[aeiou][aeiou]",line,re.I):
print(line)
推荐阅读
- three.js - Three.js rev 94 及更高版本中的 VRML 加载器
- powershell - 在 PowerShell 中通过 SFTP 获取文本文件的内容
- antlr4 - 用ANTLR分析包含单引号的引号之间的字符串
- javascript - 我目前正在尝试将任务添加到数组后清除输入字段
- mysql - MySQL全文搜索 - 最小标记大小下的布尔搜索词意味着没有结果回来
- excel - 禁用自动计算
- oracle - 将两行合并为新行并根据值求和
- c++ - 如何修复“接收到 0 个字节的 1000 毫秒后操作超时”错误?
- c# - 如何使用 CSP 全局管理员凭据在 Azure AD 中为我的租户注册应用程序?
- provisioning-profile - 使用新的捆绑标识符和配置文件构建 Testflight