python读取文本文件并找到包含两个特定单词的段落

塔塔

我有一个像下面这样的段落列表,我想提取在同一段落中包含两个特定单词的段落。

["   Electronically monitored security systems are tailored to our customers' specific needs and involve the installation and use on a\ncustomer's premises of devices designed for intrusion detection and access control, as well as reaction to various occurrences or\nconditions, such as movement, fire, smoke, flooding, environmental conditions, industrial processes and other hazards. These\ndetection devices are connected to microprocessor-based control panels, which communicate to a monitoring center (located remotely\nfrom the customer's premises) where alarm and supervisory signals are received and recorded. In most systems, control panels can\nidentify the nature of the alarm and the areas within a building where the sensor was activated. Depending upon the type of service\nfor which the subscriber has contracted, monitoring center personnel respond to alarms by relaying appropriate information to the\nlocal fire or police departments, notifying the customer or taking other appropriate action, such as dispatching employees to the\ncustomer's premises. In some instances, the customer may monitor the system at its own premises or the system may be connected to\nlocal fire or police departments.", "   Whether systems are monitored by the customer at its premises or connected to one of our monitoring centers, we usually provide\nsupport and maintenance through service contracts. Systems installed at customers' premises may be owned by us or by our customers.",'   We market our electronic security services to commercial and residential customers through both a direct sales force and an\nauthorized dealer network. A separate national accounts sales force services large commercial customers. We also utilize advertising\nand direct mail to market our services.','   We provide residential electronic security services primarily in North America, Europe and South Africa, with a growing presence\nin the Asia-Pacific region. Our commercial customers include financial institutions, industrial and commercial businesses, federal,\nstate and local governments, defense installations, and health care and educational facilities. Our customers are often prompted to\npurchase security systems by their insurance carriers, which may offer lower insurance premium rates if a security system is\ninstalled or require that a system be installed as a condition to coverage. It has been our experience that the majority of\ncommercial and residential monitoring contracts are renewed after their initial terms. In general, relocations account for the\nlargest number of residential discontinuances while business closures comprise the largest single factor impacting commercial\ncontract attrition.', "   We are the leader in anti-theft systems. The majority of the world's leading retailers use our systems to protect against\nshoplifting and employee theft. We manufacture these SENSORMATIC electronic article surveillance systems and generally sell them\nthrough our direct sales force in North and South America, Europe, Australia, Asia and South Africa. A growing trend in the loss\nprotection"]

我写了如下代码,但它没有给出我想要的。它给出了包含客户或系统的段落。

for pg in paragraphs:
    pfls = []
    pg= pg.replace("\n", ' ')
    if 'customers' in pg and 'system' in pg:
    print('1',pg)

我的代码有什么问题?

迅雷

在python中,缩进很重要。缩进描述了代码块。在您的示例中,最后三行没有正确缩进,仅在循环结束时执行,并且仅测试最后一段。

for pg in paragraphs:
    pfls = []

    pg= pg.replace("\n", ' ')
    if 'customers' in pg and 'system ' in pg:
        print('1',pg)

会达到你想要的。

编辑:这样做的一种肮脏方法是通过system在测试中的单词后添加一个空格来隔离单词,如下所示:

for pg in paragraphs:
    pfls = []

    pg= pg.replace("\n", ' ')
    if 'customers' in pg and 'system ' in pg:
        print('1',pg)

实现这一点的更好方法是使用正则表达式:

import re

systemPattern = re.compile(r"\bsystem\b")
customersPattern = re.compile(r"\bcustomers\b")

for pg in paragraphs:
    pfls = []

    pg= pg.replace("\n", ' ')
    if systemPattern.search(pg) and customersPattern.search(pg):
        print('1',pg)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从JavaScript中的文本文件读取特定单词

来自分类Dev

如何读取特定单词之间的文本文件?

来自分类Dev

从文本文件中读取包含特定单词的列

来自分类Dev

Python:读取文件时如何忽略两个特定单词之间的文本?

来自分类Dev

从文本文件中读取特定单词,然后保存下一个单词

来自分类Dev

如何读取行中特定单词的文本文件并将整行写入另一个文本文件?

来自分类Dev

搜索包含两个或两个以上带有新行的文本之间的特定单词的文件

来自分类Dev

Java同时读取两个文本文件

来自分类Dev

在大型文本文件中查找包含特定单词的行的最快方法

来自分类Dev

如何通过终端从文本文件中删除包含特定单词的行?

来自分类Dev

如何通过终端从文本文件中删除包含特定单词的行?

来自分类Dev

在大型文本文件中查找包含特定单词的行的最快方法

来自分类Dev

删除Ruby中文本文件中包含特定单词的行

来自分类Dev

如何使用 R 从文本文件中提取包含特定单词或字符的句子

来自分类Dev

比较两个文本文件并找出python中的相关单词

来自分类Dev

Python:如何检查文本文件中是否存在两个或多个给定的单词

来自分类Dev

如何使用python替换文本文件中特定单词附近的单词

来自分类Dev

过滤具有特定单词的文本文件

来自分类Dev

使用PLY的文本文件中的特定单词

来自分类Dev

计算文本文件中的特定单词-Java

来自分类Dev

从文本文件中删除特定单词

来自分类Dev

从文本文件中读取的数据分为python中的两个列表

来自分类Dev

python:读取和管理两个相关的输入文本文件

来自分类Dev

Python如何替换文本文件中特定行中的特定单词?

来自分类Dev

Python。比较两个文本文件的内容

来自分类Dev

在python中从文本文件的一行中提取特定单词

来自分类Dev

如何使用python从目录中的所有文本文件中查找特定单词

来自分类Dev

从BASH中指定单词之间的文本文件中提取特定单词

来自分类Dev

逐行读取文本文件并使用一行中的特定值计算特定单词

Related 相关文章

  1. 1

    从JavaScript中的文本文件读取特定单词

  2. 2

    如何读取特定单词之间的文本文件?

  3. 3

    从文本文件中读取包含特定单词的列

  4. 4

    Python:读取文件时如何忽略两个特定单词之间的文本?

  5. 5

    从文本文件中读取特定单词,然后保存下一个单词

  6. 6

    如何读取行中特定单词的文本文件并将整行写入另一个文本文件?

  7. 7

    搜索包含两个或两个以上带有新行的文本之间的特定单词的文件

  8. 8

    Java同时读取两个文本文件

  9. 9

    在大型文本文件中查找包含特定单词的行的最快方法

  10. 10

    如何通过终端从文本文件中删除包含特定单词的行?

  11. 11

    如何通过终端从文本文件中删除包含特定单词的行?

  12. 12

    在大型文本文件中查找包含特定单词的行的最快方法

  13. 13

    删除Ruby中文本文件中包含特定单词的行

  14. 14

    如何使用 R 从文本文件中提取包含特定单词或字符的句子

  15. 15

    比较两个文本文件并找出python中的相关单词

  16. 16

    Python:如何检查文本文件中是否存在两个或多个给定的单词

  17. 17

    如何使用python替换文本文件中特定单词附近的单词

  18. 18

    过滤具有特定单词的文本文件

  19. 19

    使用PLY的文本文件中的特定单词

  20. 20

    计算文本文件中的特定单词-Java

  21. 21

    从文本文件中删除特定单词

  22. 22

    从文本文件中读取的数据分为python中的两个列表

  23. 23

    python:读取和管理两个相关的输入文本文件

  24. 24

    Python如何替换文本文件中特定行中的特定单词?

  25. 25

    Python。比较两个文本文件的内容

  26. 26

    在python中从文本文件的一行中提取特定单词

  27. 27

    如何使用python从目录中的所有文本文件中查找特定单词

  28. 28

    从BASH中指定单词之间的文本文件中提取特定单词

  29. 29

    逐行读取文本文件并使用一行中的特定值计算特定单词

热门标签

归档