![](/img/trans.png)
[英]cat and grep from multiple files in python and write output to a file?
[英]Exporting multiple output files using grep in Python
我正在python中創建一個必須使用grep的代碼,並且在通過grep運行它時遇到了問題。 我從“ Infile”開始,然后對該文件進行剪切和排序以創建“ Infile.ids”。 “ Infile.ids”包含在“ Infile”中的唯一ID。 然后,我必須逐行通過“ Infile”從“ Infile.ids”中運行ID,並將所有具有ID的行提取到新的單獨文件中。 問題是當我在grep中運行它時,它會一次運行所有行,並且基本上給了我一堆與原始“ Infile”相同的文件,而不是單獨的唯一文件。
這些是示例“ 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
循環會更合適(盡管我懷疑在這種情況下建議這樣做)。
干杯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.