社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

scofx • 3 年前 • 1162 次点击  

我使用jupyter notebook with pandas,我想在一个大文件中找到我选择的一个重复出现的单词,然后选择行并将其粘贴或附加到另一个文本文件中,例如使用单词“ 测验 ".:

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

然后在一个新的文本文件中,只显示 测验 “目前:

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

我如何在python中使用jupyter实现这一点以使事情变得更简单?

另外,如果你能从多个文本文件中读取并附加行而不覆盖它们,那将是完美的!

一如既往地谢谢你!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129344
 
1162 次点击  
文章 [ 1 ]  |  最新文章 3 年前
mozway
Reply   •   1 楼
mozway    3 年前

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

                                 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