繁体   English   中英

Python在一行中查找单词并将其附加到新的文本文件中

[英]Python find word in a rows and append this in a new text file

我将 jupyter notebook 与 pandas 一起使用,我想在一个大文件中找到我选择的重复出现的单词,然后选择行并粘贴它或附加到另一个文本文件上,例如使用单词“ test ”。:

this is a test sample line
this is a second example line
this is a third example line
this is a test fourth sample line
this is a final example line

并在一个新的文本文件中只出现“ test ”一词的行:

this is a test sample line
this is a test fourth sample line

我如何使用 jupyter 在 python 中实现这一点,让事情变得更容易?

附注。 如果您可以从多个文本文件中读取并附加行而不覆盖它们,那将是完美的!

一如既往的感谢!

假设以下数据帧作为输入:

                                 col
0         this is a test sample line
1      this is a second example line
2       this is a third example line
3  this is a test fourth sample line
4       this is a final example line

你可以使用str.contains

df[df['col'].str.contains(r'\btest\b', regex=True)]

输出:

                                 col
0         this is a test sample line
3  this is a test fourth sample line

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM