在Python中使用grep导出多个输出文件
问题描述:
我在python中创建了一个代码,它必须使用grep,而且我在通过grep运行时遇到问题。我先从'Infile'开始,然后剪切并分类该文件以创建'Infile.ids'。 'Infile.ids'包含'Infile'中的独特ID。然后,我必须从'Infile.ids'行逐行运行'Infile',并将所有带有id的行提取到新的单独文件中。问题是,当我在grep的运行它,它运行在一旦所有的线,基本上给我回了一堆等同于原来的“INFILE”,而不是单独的独特文件的文件。在Python中使用grep导出多个输出文件
这些都是我试图获得的示例'Infile'和输出文件。
Infile Infile.ids Infile.Hello Infile.World Infile.Adios
Hello 1 3 5 7 Hello Hello 1 3 5 7 World 2 4 6 8 Adios 1 2 3 4
World 2 4 6 8 World Hello a b c d World e f g h Adios i j k l
Adios 1 2 3 4 Adios
Hello a b c d
World e f g h
Adios i j k l
这里是我的代码至今:
#!/usr/bin/python
import sys
import os
Infile = sys.argv[1]
os.system("cut -d \" \" -f1 %s | sort -u > %s.ids" % (Infile, Infile))
Infile2 = "%s.ids" % Infile
handle = open("%s.ids" % Infile, "r")
line = handle.readline()
for line in handle:
os.system("grep \"%s\" %s > %s.%s" % (line, Infile, Infile, line))
line = handle.readline()
handle.close()
答
当你遍历handle
,每line
将在后面的新行,这在Infile
行显然不(他们首先有“1 3 5 7”的东西)。所以这就是为什么你的grep失败了。
尝试做
for line in handle.readlines():
line = line.strip()
os.system("grep \"%s\" %s > %s.%s" % (line, Infile, Infile, line))
并同时删除line = handle.readline()
语句 - 如果你正在做一个for
循环,它会遍历读取线本身。如果你想使用明确的阅读电话,那么while
循环会更合适(虽然我怀疑在这种情况下建议)。
干杯
+0
非常感谢 – ajf785 2013-04-08 15:26:55
“python中的代码,其中使用grep” - 谨慎解释为什么? – georg 2013-04-08 09:30:40
让Python自己处理你在shell中调用的所有匹配语句可能会好得多。这对Python的功能有点歧视;) – 2013-04-08 09:34:34
它是一个任务的一部分,涉及在Python中使用UNIX命令。这部分需要使用grep,并且是唯一给我带来问题的部分。 – ajf785 2013-04-08 09:55:40