سؤال

How can I pass this output into a python map object?
Basically, I'd like to be able to run something similiar to print data.Data in the python script.
So that on the terminal, all that will be printed is

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

This is my python script:

[root@server tools]# cat remoteTest.py
import sys

data = sys.stdin.read()
print data

This is how I will run the command:

[root@server 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.

    }
  ]
}
هل كانت مفيدة؟

المحلول

If you are interested only in line

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

You can use subprocess module to call staf program

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

and use regular expression. I don't like of regex, but sometimes it is needed.

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

Edited with information of multline staf program output

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

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

Edited

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()

نصائح أخرى

This works for your sample. It's got some hackery to handle the nested array, which means it won't work if the nested array isn't outputted exactly like this:

Key: [
  <Nested content>

There's probably some other minor changes to the input that will break it, but its a start, at least.

#!/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']

Output:

dan@dantop:~> ./parse.py 
'uname' is not recognized as an internal or external command, operable program or batch file.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top