python - 使用单词列表作为引用的正则表达式提取,Python

标签 python regex list

我在 txt 文件中有一个介词列表。我正在创建一个函数,以便它将从字符串中提取介词后面的单词。由于介词较多,直接放入re.compile中不太可行。所以我使用的是txt文件。这是我的代码:

with open("Input.txt"):
words = "|".join(line.rstrip() for line in open)
pattern = re.compile('{}\s(\w+|\d+\w+)\s\w+'.format(words))

其中 {} 表示 preps 的匹配,而\s 是一个空格,后跟一个单词或数字和单词的组合,如 20th cross 等。我收到的错误是

TypeError                                 Traceback (most recent call last)
<ipython-input-43-0aed517ef1ba> in <module>()
  1 with open("Input.txt"):
----> 2     words = "|".join(line.rsplit() for line in open)
  3 pattern = re.compile("{}\s(\w+|\d+\w+)\s\w+".format(words))

TypeError: 'builtin_function_or_method' object is not iterable

Input.txt 文件的内容为 ['near','above','towards'...] 等等。我如何迭代它?

最佳答案

代码正在迭代open函数。您应该交互文件对象来获取行。

并且 rsplit 似乎是 rstrip 的拼写错误。

with open("Input.txt") as f:
    words = "|".join(line.rstrip() for line in f)
    pattern = re.compile(r'(?:{})\s(\w+|\d+\w+)\s\w+'.format(words))

如果单词中包含一些在正则表达式中具有特殊含义的字符,则应使用re.escape对其进行转义.

with open("Input.txt") as f:
    words = "|".join(re.escape(line.rstrip()) for line in f)
    pattern = re.compile(r'(?:{})\s(\w+|\d+\w+)\s\w+'.format(words))

关于python - 使用单词列表作为引用的正则表达式提取,Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22059434/

相关文章:

python - Python 的 MySQL 模块

正则表达式 - 不包含某些字符

python - 扫描txt,将某些数据 append 到Python中的空列表

python - 有没有办法轻松地将 2 个索引之间的所有元素放入 Python 中的嵌套列表中?

python - 在 Fedora 中使用 pip 或 dnf 安装 python 包?

python - pip 报告已安装的模块的导入错误

java - 用 Python/Java 翻录音频 CD

jQuery:用跨度包裹文本的一部分

php正则表达式删除数字

Python 从列表中删除数据以添加到新列表中