簡體   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