python - 计数和突出显示的问题

标签 python python-3.x

我正在尝试创建两个函数。一种使用两个参数,一个文件名和一个关键字。它需要突出显示单词并返回突出显示的单词或带有行号的单词。

第二个函数计算特定单词在文件中出现的次数。这个再次使用两个参数:文件名和关键字。

这个叫做高亮。它需要查看每一行,找到关键字,然后返回行号,其中关键字突出显示,左侧用“-->”括起来,右侧用“<--”括起来。

def highlight(filename, keyword):
    inpt = open(filename, "r")
    for line in inpt:
        if re.match(keyword, line):
            print ('-->',line,'<--')

此函数使用文件名和关键字,计算特定单词在文件中出现的次数,然后返回计数。

def count_word(filename, keyword):
    fname = (filename)
    word= (keyword)
    count = 0
    with open(fname, 'r') as in_file:
        for line in in_file:
            words = line.split()
            for i in words:
                if(i==word):
                    count=count+1
                print (count)

我的方向完全错误吗?我真的很亲近吗?

文本文件内容如下:

I heart Rocket!
Rocket, Rocket, Rocket.
Don't say it.
Rocket, Rocket, Rocket!





Rocket!!!

现在我得到:

highlight:
printed:
--> Rocket, Rocket, Rocket.
 <--
--> Rocket, Rocket, Rocket!
 <--
--> Rocket!!!
 <--

expected:
1:   I heart -->Rocket<--!
2:   -->Rocket<--, -->Rocket<--, -->Rocket<--.
4:   -->Rocket<--, -->Rocket<--, -->Rocket<--!
10:  -->Rocket<--!!!


count_word:

printed:
0
0
0
0
0
0
0
0
0
0
0
0
0

expected:
8

最佳答案

如果您迭代文件中的行,则每行以换行符 '\n' 结尾。

如果您在前面加上 '-->'并附加'<--''<--'添加到换行符之后。您可以更改代码,删除\n 并添加 <-- .. 但它可以更容易完成:

创建文件:

with open("k.txt","w") as f:
    f.write("""I heart Rocket!
Rocket, Rocket, Rocket.
Don't say it.
Rocket, Rocket, Rocket!





Rocket!!!""")

处理文件:

with open("k.txt") as f:

    # read all text
    text = f.read()

    # replace Rocket with -->Rocket<-- and store as text2
    text2 = text.replace("Rocket","-->Rocket<--")

    # split at \n, enumerate result starting at 1, remove any line 
    # that does not contain Rocket, join with \n
    text3 = text2.split("\n")
    text4 = [f"{row:2d} {x}" for row,x in enumerate(text3,1) if "Rocket" in x]
    text5 = "\n".join(text4)

    # Count rockets and print replaced text
    print(text.count("Rocket")) 
    print(text3)
    print(text4)
    print(text5)

输出:

# the rocket count
8  

# text3: all lines
['I heart -->Rocket<--!', '-->Rocket<--, -->Rocket<--, -->Rocket<--.', "Don't say it.", 
'-->Rocket<--, -->Rocket<--, -->Rocket<--!', '', '', '', '', '', '-->Rocket<--!!!']

# text4: with enumeration of line number 
[' 1 I heart -->Rocket<--!', ' 2 -->Rocket<--, -->Rocket<--, -->Rocket<--.', 
 ' 4 -->Rocket<--, -->Rocket<--, -->Rocket<--!', '10 -->Rocket<--!!!']

 # joined together again 
 1 I heart -->Rocket<--!
 2 -->Rocket<--, -->Rocket<--, -->Rocket<--.
 4 -->Rocket<--, -->Rocket<--, -->Rocket<--!
10 -->Rocket<--!!!
<小时/>

如果你想迷惑自己,你可以用 4 行代码来做:

with open("k.txt") as f:
    text = "\n".join( 
        [f"{row:2d} {x}" for row,x 
         in enumerate( f.read().replace("Rocket","-->Rocket<--").split("\n"),1) 
         if "Rocket" in x])
    print(text.count("Rocket")) 
    print(text)

关于python - 计数和突出显示的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55386983/

相关文章:

python - 使用 CMake 构建与平台无关的目录结构

python - 未知错误/崩溃 - 带 GPU 的 TensorFlow LSTM(第一个纪元开始后无输出)

当操作数为负数时,Python 除法运算符表现得很奇怪

python - 如何接收 cmd 'quser' 输出?

python - 将用户指定的数字添加到文件中已有的数字。 (Python)

python - 我想在我的第一个 python 环境中从第二个 python 环境调用函数。这可能吗?

python - 交换两个数字的替代方法

python - 为什么 `__new__` 返回的对象是可变的,即使对于不可变的基类也是如此?

python - 如何根据特定要求比较两个列表?

python - 使用 gensim 将 LDA 应用于语料库进行训练