简体   繁体   English

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

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

I use jupyter notebook with pandas, I would like to find a recurring word of my choice in a large file, then select the rows and paste it or append on another text file, eg with the word " test ".:我将 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

and get on a new text file only the lines in which the word " test " is present:并在一个新的文本文件中只出现“ test ”一词的行:

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

How could I achieve this in python using jupyter to make things easier?我如何使用 jupyter 在 python 中实现这一点,让事情变得更容易?

PS.附注。 it would be perfect if you could read from multiple text files and append the rows without overwriting them!如果您可以从多个文本文件中读取并附加行而不覆盖它们,那将是完美的!

Thanks as always!一如既往的感谢!

Assuming the following dataframe as input:假设以下数据帧作为输入:

                                 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

You could use str.contains :你可以使用str.contains

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

output:输出:

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

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

相关问题 在python中搜索特定单词后,将一个单词添加到文本文件中 - Append a text word into a text file after searching for specific word in python 如何打开文本文件并查找在一行中的特定单词和 append 文件名之后写入的内容到 Python 中的列表 - How to open a text file and find what is written after a specific word in a line and append that files name to a list in Python 在python中查找文本文件中每个单词的频率 - To find frequency of every word in text file in python 将下一个单词添加到python中文本文件的列表中,并按列打印 - Append next word into a list from a text file in python and print it columnwise 在文本文件中查找单词 - Find word in text file 在python中附加文本文件 - Append text file in python 如何使用python将值作为新列附加到现有文本文件中 - How to append values as a new coloumn to existing text file using python 替换文本文件中的单词并将其写入python中的新文件 - Replace word in text file and writing it to a new file in python Python-如何在新Word文件上的所需坐标上添加文本 - Python- How to add text on desired coordinates on new word file 在文本文件中查找整数在新文本文件中重写为字符串 Python - Find integer in text file rewrite in new text file as string Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM