1

我正在提取文件中以“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 程序中使用的替代方法。

4

2 回答 2

1

你可以看看重新:http ://docs.python.org/2/library/re.html

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

于 2013-06-10T21:23:53.543 回答
1

在 Windows 中试试这个:

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

Windows 的 grep需要"双引号"

于 2013-06-11T07:24:05.720 回答