简体   繁体   中英

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 ".:

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:

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?

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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