Thursday, 7 January 2021

Python script to convert json to csv

 import json

import csv

import time

import logging

import datetime as dt


json_file_path = "D:\.json"

csv_file_path = "D:\.csv"

global_key = []

global_val = []

final_values = [{}]


print ('Device Details script  started at:%s'%(dt.datetime.utcnow()))


def to_string(s):

    try:

        return str(s)

    except:

        return s.encode('utf-8')

def set_final_values(final_key, final_val, path_key_str):

    flag = 0

    b = 0

    for a in final_key:

        try:

            b = int(a)

            flag = 1

            try:

                len((final_values)< b+1)

            except:

                final_values.append({})

        except:

            continue

    logging.debug("Keys and Values updated and assigning to final_values list")

    final_values[b][path_key_str] = final_val


def fetch_key_value(input_json, path_key = None, path_val = None, last_key = None):

    if path_key is None:

        path_key = []            

        path_val = []


    if last_key is not None:

        path_key = path_key + [last_key]

       

    if type(input_json) is dict and input_json:

        for key, val in input_json.iteritems():

            if(type(val) is not dict and type(val) is not list):

                path_val = path_val + [to_string(val)]

            if type(input_json[key]) is dict or type(input_json[key] is list):

                path_key, path_val = fetch_key_value(input_json[key], path_key, path_val, key)

        if len(path_key)>0:

            path_key.pop()

    elif type(input_json) is list and input_json:

        for entity in input_json:

                path_key, path_val = fetch_key_value(entity, path_key, path_val, input_json.index(entity))

        if len(path_key)>0:        

            path_key.pop()

    else:

        if path_key:

            global global_key

            global global_val

            val = []

            val = path_val

            tmp_val = ""

            path_key_str = ""

            for i in path_key: 

                if(path_key_str == ""):

                    path_key_str = to_string(i)

                else:

                    if not isinstance(i,int):         

                        path_key_str = path_key_str + "_" + to_string(i) 

            global_key = global_key + [path_key_str]

            if val:

                tmp_val = str(val.pop())

                set_final_values(path_key, tmp_val, path_key_str)

            path_key.pop()

            global_val = global_val + [tmp_val]

    return path_key, path_val 

def enter_header(header):

    final_list = []

    for x in header:

        if x not in final_list:

            final_list.append(to_string(x))

    print("Header added.")

    logging.debug("Header added.")

    return final_list 


def enter_csv_row(rows, csvwriter):

    for x in rows:

        if x != {}:

            csvwriter.writerow(x)

    print("Rows added.")

    logging.debug("Rows added.")


def format_data(local_key, local_values):

    with open(csv_file_path,'wb') as csv_file:

        header = enter_header(local_key)    

        csvwriter = csv.DictWriter(csv_file, fieldnames = header)

        csvwriter.writeheader()

        enter_csv_row(local_values, csvwriter)

try:

    with open(json_file_path, 'r') as json_file:

        json_data = json.load(json_file)

        json_keys, jason_vals = fetch_key_value(json_data)

        format_data(global_key, final_values)

        print("CSV created successfully.")

        logging.debug("CSV created successfully.")

except Exception as e:

    print ("Exception =>" + str(e))

    logging.debug("Exception =>" + str(e))

Monday, 4 January 2021

Home loan process

 

Fill The Loan Application Form & Attach The Documents

Pay The Processing Fee

Discussion With The Bank

Valuation Of The Documents

The Sanction/Approval Process

Processing The Offer Letter

Processing The Property Papers Followed By A Legal Check

Processing A Technical Check & The Site Estimation

The Final Loan Deal

Signing The Agreement

The Loan Disbursal

Monday, 14 December 2020

Python regex to match start and end of sentence

 


#Check if the string starts with "The" and ends with "chennai":

txt = "The rain in chennai"

x = re.search("^The.*chennai$", txt)

Saturday, 28 November 2020

Why do we need weekend ??

 

  1. To improve on yourself as a person rather than always improving your professional skills.
  2. To re-energize yourself, when you go back after weekend, you are more relaxed and productive. A couple of days are really required to forget the rudeness of your office colleagues and managers.
  3. To spend time with friends and family, and to plan a weekend trip in a nearby place. It is also important to keep strengthening the bonds with the others around you, and these days you will always remember even when you retire.
  4. To spend time alone and learn some new skills. You can meditate and learn about spirituality, or you can enhance your skills in something else, which can help you, if the current technology you are working in, goes outdated. You will come out to be a more confidant and secured person.

Monday, 26 October 2020

Hackathon flow

 


Flow 

Register -> Create Teams -> View Problems/Deadlines/Leaderboard -> Submit Solutions -> Results

Monday, 3 August 2020

Ansible tower / AWX notification Microsoft teams channel



It's not a http header, it's the payload, which you can't alter. 

A WebHook Notification template type won't work for Mattermost/MS Teams/RocketChat or any other Slack compatible communication platform.

Try selecting Mattermost as the notification type and point it to your Teams webhook url; This should do the trick.

Git

1 git add ↳ It lets you add changes from the working directory into the staging area 2 git commit ↳ It lets you save a snapshot of currently...