Python 在一行中查找单词并将其追加到新的文本文件中
Python find word in a rows and append this in a new text file
我将 jupyter notebook 与 pandas 一起使用,我想在一个大文件中找到我选择的重复出现的单词,然后 select 行并将其粘贴或附加到另一个文本文件,例如,单词“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
我如何在 python 中使用 jupyter 使事情变得更容易?
PS。如果您可以从多个文本文件中读取并附加行而不覆盖它们,那就太完美了!
一如既往的感谢!
假设以下数据框作为输入:
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
我将 jupyter notebook 与 pandas 一起使用,我想在一个大文件中找到我选择的重复出现的单词,然后 select 行并将其粘贴或附加到另一个文本文件,例如,单词“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
我如何在 python 中使用 jupyter 使事情变得更容易?
PS。如果您可以从多个文本文件中读取并附加行而不覆盖它们,那就太完美了!
一如既往的感谢!
假设以下数据框作为输入:
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