解析盐堆高州输出

时间:2018-01-27 13:47:41

标签: python parsing salt-stack

我走了路径python 现在,我需要解析输出数据saltstack
例如:

{"fun_args": [], "jid": "20180127110016597705", "return": {"biosversion": "0407\\nInvalid entry length (16). Fixed up to 11.", "kernel": "Linux", "domain": "corp.test.com", "uid": 0, "zmqversion": "4.1.4", "kernelrelease": "4.10.0-42-generic", "pythonpath": ["/usr/bin", "/usr/lib/python2.7", "/usr/lib/python2.7/plat-x86_64-linux-gnu", "/usr/lib/python2.7/lib-tk", "/usr/lib/python2.7/lib-old", "/usr/lib/python2.7/lib-dynload", "/usr/local/lib/python2.7/dist-packages", "/usr/lib/python2.7/dist-packages", "/usr/lib/python2.7/dist-packages/gtk-2.0"], "serialnumber": "System Serial Number\\nInvalid entry length (16). Fixed up to 11.", "pid": 29227, "ip_interfaces": {"lo": ["127.0.0.1", "::1"], "enp6s0": ["10.0.1.83", "fe80::758:bc44:76a7:9bd6"]}, "groupname": "root", "fqdn_ip6": [], "mem_total": 3861, 

现在我把这个数据作为一个元素的元组 我可以将元组转换为dict,但它也有一个元素。

我想解析这些数据,就像这样(字典):

{"fun_args": [], "jid": "20180127110016597705", 
"return": 
    {
    "biosversion": "0407\\nInvalid entry length (16). Fixed up to 11.", 
    "kernel": "Linux", 
    "domain": "corp.test.com", 
    "uid": 0, 
    "zmqversion": "4.1.4", 
    "kernelrelease": "4.10.0-42-generic", 
    "pythonpath": ["/usr/bin", "/usr/lib/python2.7", "/usr/lib/python2.7/plat-x86_64-linux-gnu", "/usr/lib/python2.7/lib-tk", "/usr/lib/python2.7/lib-old", "/usr/lib/python2.7/lib-dynload", "/usr/local/lib/python2.7/dist-packages", "/usr/lib/python2.7/dist-packages", "/usr/lib/python2.7/dist-packages/gtk-2.0"], 
    "serialnumber": "System Serial Number\\nInvalid entry length (16). Fixed up to 11.", 
    "pid": 29227, 
    "ip_interfaces": {
                    "lo": ["127.0.0.1", "::1"], 
                    "enp6s0": ["10.0.1.83", "fe80::758:bc44:76a7:9bd6"]
                    }, 
    "groupname": "root", 
    "fqdn_ip6": [], 
    "mem_total": 3861, 

感谢名单。 我试着这样说:

    #!/usr/bin/env python

import mysql.connector
from mysql.connector import Error

def connect():
    """ Connect to MySQL database """
    try:
        conn = mysql.connector.connect(host='10.0.0.125',
                                       database='salt',
                                       user='salt',
                                       password='salt')
        if conn.is_connected():
            #print('Connected to MySQL database')
            cursor = conn.cursor()
            cursor.execute("SELECT full_ret FROM salt_returns WHERE fun='grains.items'")

            row = cursor.fetchone()

            s = ''.join((row[0]))
            #print(s.split())
            list = s.split()
            #print(list)
            for i in list:
                print(i)

    except Error as e:
        print(e)

    finally:
        conn.close()

1 个答案:

答案 0 :(得分:0)

#!/usr/bin/env python

import mysql.connector
import json
from mysql.connector import Error


def connect():
    """ Connect to MySQL database """
    try:
        conn = mysql.connector.connect(host='10.0.0.125',
                                       database='salt',
                                       user='salt',
                                       password='salt')
        if conn.is_connected():
            cursor = conn.cursor()
            cursor.execute("SELECT full_ret FROM salt_returns WHERE fun='grains.items'")
            row = cursor.fetchone()

            s = ''.join((row))
            obj = json.loads(s)

            s2 = obj['return']
            for i in s2:
                print(i, s2[i])
                if type(s2[i]) is dict:
                    for i2 in s2[i]:
                        print(i2, s2[i][i2])
                elif type(s2[i]) is list:
                    for i3 in s2[i]:
                        print(i3)
    except Error as e:
        print(e)

    finally:
        conn.close()

if __name__ == '__main__':
    connect()