Python - 在线搜索单词、计算它、添加它
我很确定我想得太多了,而且有一个简单的结果,但我似乎无法将它们放在一起。
我正在寻找一种搜索方法。我想要一个 Python 脚本在文本文件中搜索关键字并计算它出现的行数。尽管如果关键字在一行中多次出现,我仍然想只计算一次。
长话短说;如果键盘出现在一行上,我希望它计算该行并将其添加到数学方程中。
非常感谢任何帮助!提前致谢。
I'm pretty sure I'm over thinking this and there's a simple outcome for it, but I just can't seem to put it all together.
I'm looking for a kind of a search method. I'd like a Python script search a text file for a keyword and count how many lines it appears on. Though if the keyword comes up on a single line multiple times, I'd like to still only count it once.
Long story short; If a keyboard comes up on a single line, I want it to count that line and add it to what will be a math equation.
Any help is greatly appreciated! Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以定义以下函数。
现在,如果您想知道“foo.cpp”中包含“int”的行数,您可以:
您可以在命令行上执行的另一种选择(如果您在适当的平台上)是:
You can define the following function.
Now if you want to know the number of lines containing "int" in "foo.cpp", you do:
An alternative that you can do on the command line (if you are on an appropriate platform) is:
非 Python Unix 解决方案相当直接:
您实现起来有困难吗Python 中这两者的本质是什么?
A non-Python Unix solution is fairly immediate:
Do you have difficulty implementing the essence of either of these in Python?
假设f是文件对象,
Assuming f is the file object,
也许你可以尝试这个:
Perhaps you could try this: