Posts

Showing posts from May, 2018

Converting Excel File into a Dictionary using xlrd module

from xlrd import open_workbook import json import os os.chdir("d:/2018/mayank") def parse_xlsx():     workbook = open_workbook('demo.xlsx')     sheets = workbook.sheet_names()     active_sheet = workbook.sheet_by_name(sheets[0])     num_rows = active_sheet.nrows     num_cols = active_sheet.ncols     header = [active_sheet.cell_value(0, cell).lower() for cell in range(num_cols)]     for row_idx in range(1, num_rows):         row_cell = [active_sheet.cell_value(row_idx, col_idx) for col_idx in range(num_cols)]         yield dict(zip(header, row_cell)) for row in parse_xlsx():     print (row)

Converting Excel File to JSON File using xlrd module

from xlrd import open_workbook import json import os os.chdir("d:/2018/mayank") book = open_workbook('demo.xlsx') sheet = book.sheet_by_index(0) # read header values into the list keys = [sheet.cell(0, col_index).value for col_index in range(sheet.ncols)] print ("keys are", keys) dict_list = [] for row_index in range(1, sheet.nrows):     d = {keys[col_index]: sheet.cell(row_index, col_index).value          for col_index in range(sheet.ncols)}     dict_list.append(d) j = json.dumps(dict_list) with open('data.json', 'w') as f:     f.write(j)

Reading data from Web Service using Python

#import the modules import requests import json # Get the feed r = requests.get("http://msme.world/getmembersjson.php") print(r.text) data = json.loads(r.text) print(data)

Reading Excel File in Python

Image
Sample Excel Sheet import pandas as pd f = pd.ExcelFile("customers.xlsx") dfs = {sheet_name: f.parse(sheet_name)           for sheet_name in f.sheet_names} s=dfs["Sheet1"] #print(s) #print column names only for r in s:     print(r,end=" ") print() #print the column data for i in range(len(s)):     for r in s:         print(s.at[i,r],end=' ')     print() i=0    Output

Dynamically Reading JSON Data

{ "supplier": { "supp_name": "", "supp_id": "", "address": { "addressLine1": "", "Addressline2": "", "Postcode": "" }, "Billing_address": { "addressLine1": "", "Addressline2": "", "AddressLine3": "" }, "CurrencyCode": "", "CountryCode": "" } } import json f=open("supplier.json") data=f.read() jd=json.loads(data) tn=list(jd.keys())[0] for k,v in jd[tn].items():     if len(v)==0:         print(k)     else:         print(k)         for x,y in v.items():             print(" ",x)            

Using DB2 with Python

Download the package pip install ibm_db Import the module         import ibm_db Connect to a local or cataloged database cn = ibm_db.connect("database","username","password") Connect to an uncataloged database cn=ibm_db.connect("DATABASE=name;HOSTNAME=host;PORT=60000;PROTOCOL=TCPIP;UID=username;                 PWD=password;", "", "")

Using Oracle with Python

Install the module     pip install cx_Oracle Import the module     import cx_Oracle Establish the connection     cn = cx_Oracle.connect('userid/password@servername') #rest commands are same as MySQL

OOPs in Python

Components of a class 1.     Data members 2.     Constructor a.      __init__() 3.     Setter to update the value 4.     Getter to return the value 5.     General methods Example Create a class Customer having data members as cid, name and balance. Create a constructor to initialize data in the data members. Create required setter and getter Create a method deposit() to deposit some amount Create a method withdraw() to withdraw some amount Solution 1: Fixed data class Customer:     def __init__(self,cid,name,opamount):         self.cid=cid         self.name=name         self.balance=opamount     def setName(self,name):         self.name=name     def getCid(self): ...