2014-05-13 43 views
0

如何將此輸出傳遞給python地圖對象?
基本上,我希望能夠在python腳本中運行類似於print data.Data的東西。
所以在終端上,所有將要打印的是基於bash輸出創建python地圖

'uname' is not recognized as an internal or external command, operable program or batch file. 

這是我的Python腳本:

[[email protected] tools]# cat remoteTest.py 
import sys 

data = sys.stdin.read() 
print data 

這是我將如何運行命令:

[[email protected] tools]# staf server2.com PROCESS START SHELL COMMAND 'uname' WAIT RETURNSTDOUT STDERRTOSTDOUT | python remoteTest.py 
Response 
-------- 
{ 
    Return Code: 1 
    Key  : <None> 
    Files  : [ 
    { 
     Return Code: 0 
     Data  : 'uname' is not recognized as an internal or external command, 
operable program or batch file. 

    } 
    ] 
} 
+0

你控制'staf'的輸出?輸出格式的一個更好的選擇(它看起來有點像JSON)會使它更容易。 – jedwards

+0

我沒有,'staf'輸出超出了我的控制範圍。它將始終遵循這種格式 – bobbyrne01

+0

您最好的機會是嘗試將「幾乎json」部分轉換爲'ast.literal_eval'可以解析的格式,這意味着圍繞鍵(返回碼,鍵,文件等)和引號中的所有其他字符串值,並在每個映射條目之間放置逗號。這很難以通用的方式來完成,但是如果你可以對工作人員輸出的格式做出某些假設,那麼這可能是可行的。 – dano

回答

1

如果您只對

Data  : 'uname' is not recognized as an internal or external command, operable program or batch file. 
感興趣

您可以使用subprocess module調用STAF程序

import subprocess 
output = subprocess.check_output(["staf", "server2.com PROCESS START SHELL COMMAND 'uname' WAIT RETURNSTDOUT STDERRTOSTDOUT"]) 

,並使用正則表達式。我不喜歡正則表達式,但有時需要它。

result = re.findall(r'Data\s+:\s+(.*)', output, re.M)[0] 
print result 

編輯 與multline STAF程序輸出的信息

output = output.replace('\n', '') 

result = re.findall(r'Data\s+:\s+(.*)}', output, re.M)[0] 

編輯

o = """{ 
    Return Code: 1 
    Key  : <None> 
    Files  : [ 
    { 
     Return Code: 0 
     Data  : 'uname' is not recognized as an internal or external command, 
operable program or batch file. 

    } 
    ] 
}""" 
a = o.replace('\n', '') 
import re 
print re.findall('Data\s+:\s+(.+?)\}', a)[0].strip() 
+0

這不起作用,在文件「/root/Python-2.7.6/Lib/subprocess.py」,第573行,在check_output中引發CalledProcessError(retcode,cmd,output = output)subprocess.CalledProcessError:Command'['staf ','server2.com PROCESS START SHELL COMMAND'uname'WAIT RETURNSTDOUT STDERRTOSTDOUT「]'返回非零退出狀態1' – bobbyrne01

+0

也許你必須傳遞每個參數分開。 subprocess.check_output(['staf','server2.com','PROCESS','START','SHELL','COMMAND',''uname'','WAIT','RETURNSTDOUT','STDERRTOSTDOUT']) – andmart

+0

分離每個參數後,這是響應。注意不包含所有輸出:''uname'不被識別爲內部或外部命令,' – bobbyrne01

0

這適用於你的樣品。它有一些兩輪牛車來處理嵌套的數組,這意味着如果嵌套數組不輸出完全一樣,它不會工作:

Key: [ 
    <Nested content> 

有可能的輸入,這將打破它的一些其他小的變化,但至少它是一個開始。

#!/usr/bin/python 

import ast 

a = """ 
Response 
-------- 
{ 
    Return Code: 1 
    Key  : <None> 
    Files  : [ 
    { 
     Return Code: 0 
     Data  : 'uname' is not recognized as an internal or external command, operable program or batch file. 

    } 
    ] 
}""" 

lines = a.split("--------")[1].split('\n') 
new = [] 
for line in lines: 
    if ":" in line: 
     # If the line is a mapping, split it into the part before and after the ":". 
     parts = [l.strip() for l in line.split(":")] 
     # Surround strings in double quotes. 
     s = ': '.join(['"%s"' % i if not i.startswith("[") else i for i in parts]) 
     # Add a comma if it's not an embedded list. 
     if not s.endswith('['): 
      s += "," 
     line = s 
    if line: 
     new.append(line) 

d = ast.literal_eval('\n'.join(new)) 
print d['Files'][0]['Data'] 

輸出:

[email protected]:~> ./parse.py 
'uname' is not recognized as an internal or external command, operable program or batch file. 
+0

你如何建議我將數據加載到python腳本而不是硬編碼字符串? – bobbyrne01

+0

@ bobbyrne01您可以使用您在示例中顯示的'sys.stdin.read()'方法。您也可以使用'subprocess'模塊直接從腳本調用'staf'。 – dano

+0

使用'data = sys.stdin.read()'並用'data'代替'a',我得到了'Traceback(最近調用最後一個):文件「remoteTest.py」,第23行,在 d = ast.literal_eval('\ n'.join(new))文件「/root/Python-2.7.6/Lib/ast.py」,第49行,在literal_eval node_or_string = parse(node_or_string,mode ='eval')文件「/root/Python-2.7.6/Lib/ast.py」,第37行,解析返回編譯(源文件名,模式,PyCF_ONLY_AST)文件「」,第8行可操作程序或批處理文件。 – bobbyrne01