首页 > 解决方案 > Re.search 返回无,无法弄清楚出了什么问题

问题描述

作为初学者,如果答案对某些人来说是显而易见的,我深表歉意。

我编写了一个代码来提取特定关键字并按照它们在数据集中或文本中的顺序列出它们。最终,我得到了一个“错误”,例如,re.search 没有从测试文件中找到任何内容,即使它确实有几个搜索过的单词。重新进口

datatofilter = open("S:/Töö/Meilikood/Meilide txtd/veb 21/test1.txt", "r")
linecount = 1
setcount = 0
set = ["one", "two"]
text = datatofilter.readline()
while text:
    text = datatofilter.readline(linecount)
    match = 0
    fromset = set[setcount]
    linesearch = re.search(fromset, text)
    #if (linesearch == None) is False:
    print(linesearch)
        #match = 1
    setcount =+ 1
    linecount =+ 1
    if text == "stopcode":
        break
datatofilter.close()

“test1.txt”正是:

one, two
2
3
one, two
one
stopcode

尝试运行代码正好打印了 30 行“无”(没有撇号)

搜索了 Stack Overflow 并找到了几个与我的问题类似的线程,我仍然不知道为什么它不起作用。

标签: python

解决方案


推荐阅读