Python:读取输出脚本两次并将两列写入csv

时间:2012-04-14 19:13:51

标签: python csv stdout

这是我的第一个python (2.4)程序,我可以使用你的帮助。 我有一个csv文件

HOST, PATH
server1, /path/to/file1.py
server2, /path/to/file2.py
server3, /path/to/file3.py

对第2列/第[1]行

的每个PATH执行命令

在命令的输出中,我搜索文本并将它们附加到新列。

示例命令输出:

Command: python /path/to/file1.py
Output: server1 (NTFS) Reply:Yes

我想制作我的新csv文件,如:

HOST, PATH, PLATFORM, REPLY
server1, /path/to/file1.py, Windows, Yes
server2, /path/to/file2.py, Linux, Yes
server3, /path/to/file3.py, BSD, No

我仍然习惯于阅读相同的输出行并附加两列不同的结果。我试图关闭作者并在同一个读者中打开一个新的作者,没有任何喜悦。我没有运气就试着缩进。我得到的最接近的是准确搜索两个字符串,但它只写入一列。

我还尝试首先搜索os平台并将更改写入新文件 然后,打开新文件以写入另一行

当然,我可以通过再次运行命令并单独搜索来强制执行此操作,但这是多余的和不必要的。

import datetime
import csv
import os, time
from stat import * # ST_SIZE etc
from subprocess import Popen, PIPE, STDOUT

# Set Date
now = datetime.datetime.now()
today = now.strftime("%m-%d-%Y")

# Files
filename = "my_list.csv"
results = "results/results_" + today + ".csv" # Eg. results_04-14-2012.csv

# Commands
command = "python"
SP = " "

incsv = open(filename, 'rb')
try:
    reader = csv.reader(incsv)

    outcsv = open(results, 'w')
    try:
        writer = csv.writer(outcsv)

        for row in reader:
            p = Popen(command + SP + row[1], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
            stdout, empty = p.communicate()

            print 'Command: %s\nOutput: %s\n' % (command + SP + row[1], stdout)

            if not stdout:
                writer.writerow(row + ['PLATFORM']) # Create header for new column when first line is empty on stdout
            elif 'BSD' in stdout:
                writer.writerow(row + ['BSD'])
            elif 'Linux' in stdout or 'swap' in stdout or 'LLVM' in stdout or 'EBR' in stdout:
                writer.writerow(row + ['Linux'])
            elif 'NTFS' in stdout:
                writer.writerow(row + ['Windows'])
            else:
                writer.writerow(row + ['Error Scanning'])

                    reply = open(results, 'w')
                    try:
                        writer = csv.writer(platform)

                        for row in reader:

                            if not stdout:
                                writer.writerow(row + ['REPLY']) # Create header for new column when first line is empty on stdout
                            elif 'Reply:Yes' in stdout:
                                writer.writerow(row + ['Yes'])
                            elif 'Reply:No' in stdout :
                                writer.writerow(row + ['No'])

                            else:
                                writer.writerow(row + ['Error'])

                    finally:
                        reply.close()
    finally:
        outcsv.close()
finally:
    incsv.close()

2 个答案:

答案 0 :(得分:1)

如果输入线和输出线之间有1:1的映射,则可能不需要csv。只需处理每一行并吐出一条新的。

方法1:管道

import sys
import subprocess

def parse_csv(input, output):
    # echo headings with new columns
    print >> output, input.readline() + ', PLATFORM, REPLY'

    for line in input.readlines():
        server, _, path = line.partition(',')
        path = path.strip()
        p = subprocess.Popen(command + ' ' + path,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             shell=True)
        stdout, _ = p.communicate()

        # your additional logic goes here based on contents of stdout

        # when ready to output, just print, e.g.
        print >> output, '%s, %s, %s' % (line, stdout, 'Yes')

if __name__ == '__main__':
    parse_csv(sys.stdin, sys.stdout)

执行

$ cat servers_and_paths.csv | python add_two_columns.py > servers_paths_and_flags.csv

方法2:文件

如果您不想使用管道,请使用with子句来处理打开/关闭:

from contextlib import closing

if __name__ == '__main__':
    with closing(open('servers_and_paths.csv')) as input,
         closing(open('servers_paths_and_flags.csv', 'wb')) as output:
        func_name(input, output)

答案 1 :(得分:1)

从输出中收集您想要的所有信息,并通过一次调用将其写出writerow()。如,

newdata = ['BSD', 'Yes']
...
writer.writerow(row + newdata)

当然,您将通过解析脚本结果逐步构建newdata

PS。更新你的python! 2.4太旧了。更新将以无数种方式让您的生活更轻松。