Python Forum
Data alignment in Python - Printable Version

+- Python Forum (
+-- Forum: Python Coding (
+--- Forum: General Coding Help (
+--- Thread: Data alignment in Python (/Thread-Data-alignment-in-Python)

Data alignment in Python - Nirmal - Feb-11-2019

I am new to Python . I am writhing a code to generate a excel file having the data sourced by calling API and correlate those to get desired result.

basically taking input from one database and search that in others and fetch related information.

The 4 databases have below data :

{u'data': [{u'_id': u'5c30702c8ca9f51da8178df4',
            u'encap': u'vlan-24',
            u'ip': u'',
            u'mac': u'5B:P9:01:9E:42:08'}]}

{u'data': [{u'_id': u'5c54a81a8ca9f51da84ae08e',
            u'paths': u'paths-1507',
            u'endpoint': u'eth1/10',
            u'cep': u'5B:P9:01:9E:42:08',
            u'tenant': u'ESX'}]}

{u'data': [{u'Allocation': u'Build_Reserved',
            u'address': u'',
            u'name': u'fecitrix-1',
            u'state': u'RESERVED'}]}
{u'data': [{u'_id': u'57ccce8110dd54f02881fedc', 
            u'client': u'CES',
            u'hostname': u'fecitrix-1'
            u'os_team': u'Window'}]}

1. if "mac" of EEp and "cep" of PathEp is same than take "encap","ip" ,"mac"
   "paths" ,'endpoint","cep" and "tenant" (these values need to be exported
    to excel)

2.take ip of EEp and search in "ip4_address" 
and get the "name" from ip4_address ( name need to be exported to excel).

3.if "name" of ip4_address is equal to "hostname" os asset then take 
  "client" and "os_team" ( export that to excel)
i have written the script but not getting the desired result.

def get_host_details(self):
    data = {
        "find": {
            "hostname": self.controller
        "tenant": 1,
        "paths": 1,
    host_details ="", data)
    #print host_details
    hosts = []
    for record in host_details:
        if "mig" not in record["endpoint"]:
    return hosts

def get_ipaddress(self, controller):

    host_record = {"tenant": "UNKNOWN",
                   "paths": "UNKNOWN",
                   "endpoint": "UNKNOWN",
                   "ip": "UNKNOWN",
                   "mac": "UNKNOWN",
                   "encap": "UNKNOWN"}
    data = {
        "find": {
            "hostname": controller,
            "ip": {
                "$ne": ""
        "projection": {
            "ip": 1,
    endpoints ="", data)
    IPAM = self.get_dns()
    print endpoints
    host_details = self.get_host_details()
    host_details_record = []
    for record in endpoints:
        for host in host_details:
            if record["mac"] == host["cep"]:
                host_record = {"tenant": host["tenant"],
                               "paths": host["paths"],                                  
                               "endpoint": host["endpoint"],
                               "ip": record["ip"],
                               "mac": record["mac"],
                               "encap": record["encap"]}

def get_dns(self, endpoints):
    ip_dns_record = []
    for each_endpoint in endpoints:
        data = {
                    "address": {
                        "$eq": each_endpoint["ip"]
                    "name": 1


        dns_record = {"client":"UNKNOWN",

        ipam_record =""
                                "internal/ip4_address/find", data)
        if ipam_record:
            dns_record["ip_address"] = each_endpoint["ip"]
            dns_record["hostname"] = ipam_record[0]["name"]
            dns_record = self.get_remedy_details(ipam_record[0]["name"],
            dns_record["ip_address"] = each_endpoint["ip"]
            dns_record["hostname"] = "UNKNOWN"

def get_remedy_details(self, hostname, dns_record):

    data = {
                "hostname": hostname.upper(),

    remedy_data ="", data)
    #remedy_data = remedy_data["data"]
    if remedy_data:
        dns_record["client"] = remedy_data[0].get("client","UNKNOWN")
        dns_record["os_team"] = remedy_data[0].get("os_team", "UNKNOWN")

        dns_record["client"] = "UNKNOWN"
        dns_record["os_team"] = "UNKNOWN"

    return dns_record

def get_excel(self, ip_dns_record):
    filename = self.controller + ".xls"
    excel_file = xlwt.Workbook()
    sheet = excel_file.add_sheet('HOSTLIST')
    sheet.write(0, 0, "IP Address")
    sheet.write(0, 1, "HostName")
    sheet.write(0, 2, "Client")
    sheet.write(0, 3, "OS Team")

    for count in xrange(1, len(ip_dns_record)+1):
        sheet.write(count, 0,ip_dns_record[count - 1]["ip_address"])
        sheet.write(count, 1,ip_dns_record[count - 1]["hostname"])
        sheet.write(count, 2,ip_dns_record[count - 1]["client"])
        sheet.write(count, 3,ip_dns_record[count - 1]["os_team"])
if __name__ == "__main__":
    controller = sys.argv[1]
    OBJ = ACIHostList(controller)
No idea where i am going wrong and what needs to be done. Any idea?

RE: Data alignment in Python - nilamo - Feb-12-2019

You didn't include the __init__ method, so unless that does more than just initialization, your script doesn't do anything because you aren't calling any of it's methods.