【问题标题】:alternative of grep in python running on Windows [duplicate]在Windows上运行的python中grep的替代品[重复]
【发布时间】:2013-06-06 15:27:43
【问题描述】:

我正在提取文件中以 'abc' 开头的第一行

grep -w 'abc' --max-count=1 file.tsv

我想在 python 程序中使用它

import subprocess

process = subprocess.Popen("grep -w 'abc' --max-count=1 file.tsv",
                             shell=True,
                             stdout=subprocess.PIPE,
                           )
stdout = process.communicate()[0].split('\n')

我的 python 在 Windows 上运行,grep 将无法工作。有没有可以在我的 python 程序中使用的替代方法。

【问题讨论】:

  • 所谓的重复指向 Unix,这是一个Windows 问题。

标签: python windows python-2.7 grep


【解决方案1】:

您可以查看回复:http://docs.python.org/2/library/re.html

打开文件并使用re搜索行;这将消除调用子流程的需要。

【讨论】:

    【解决方案2】:

    在 Windows 中试试这个:

    grep -w "abc" --max-count=1 file.tsv
    

    grep for Windows 需要"双引号"

    【讨论】:

      猜你喜欢
      • 2018-09-28
      • 2010-11-28
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 2012-10-05
      • 2011-08-26
      • 2011-07-23
      • 2010-09-08
      相关资源
      最近更新 更多