Numpy array
Monthly Archives: November 2019
MySQL select with where
select with where
Data uploading from csv fi
from collections import namedtupleimport csvimport mysql.connector products = namedtuple(‘products’,’Product_Id,Product_Name,Product_Type,Product_Price,Product_Mfg_date’) dbase=mysql.connector.connect(user=”root”,password=”root”,database=”testdb”) cursor=dbase.cursor() #read csv file using named tuplefor product in map(products._make, csv.reader(open(“products.csv”, ‘r’))): sqlInsert=”insert into product(Product_Id,Product_Name,Product_Type,Product_Price,Product_Mfg_date) values(‘%s’,’%s’,’%s’,%d,’%s’)” price = float(product.Product_Price) params= (product.Product_Id,product.Product_Name,product.Product_Type,price,product.Product_Mfg_date) print(sqlInsert % params) try: Continue reading “Data uploading from csv fi”
MySQL delete
Python Mysql delete operation
Python MySQL update
import mysql.connector # Data entry for updating product data pid=input(“Enter Product ID: “)pname=input(“Enter Product Name: “)ptype=input(“Enter Product Type: “)pprice=float(input(“Enter Product Price: “))pmfgdt=input(“Enter Product MFG Date:”) dbase=mysql.connector.connect(user=”root”,password=”root”,database=”testdb”) cursor=dbase.cursor() sqlUpdate=”update testdb.product set Product_Name=’%s’,Product_Type=’%s’,Product_Price=%d,Product_Mfg_date=’%s’ where Product_Id=’%s’” params=(pname,ptype,pprice,pmfgdt,pid) try: cursor.execute(sqlUpdate % params) dbase.commit() print(“Row successfully updated.”) except: dbase.rollback() Continue reading “Python MySQL update”
Special number
special number
Empty class
Python empty class
Python Chain Map
from collections import ChainMap sales = ChainMap() dvd = sales.new_child() dvd[2009]=10000dvd[2010]=15000dvd[2011]=20000 print(‘DVDs Yearly Sales: ‘,dvd.maps[0]) cd = sales.new_child() cd[2009]=99000cd[2010]=45000cd[2011]=20000 print(‘CDs Yearly Sales: ‘,cd.maps[0]) blueray = sales.new_child() blueray[2009]=99000blueray[2010]=45000blueray[2011]=20000 print(‘BlueRay Yearly Sales: ‘,blueray.maps[0]) print(‘Dvd: ‘,dvd.items()) print(‘—————————————————‘) spring= {‘March’:18,’April’:19,’May’:20}summer = {‘June’:23,’July’:26,’Aug’:27}fall = {‘Sept’:20,’Oct’:18,’Nov’:16}winter = {‘Dec’:2,’Jan’:1,’feb’:5} temp = ChainMap(spring,summer,fall,winter) print(‘March Temp: ‘,temp[‘March’]) print(‘Dec Temp: ‘,temp[‘Dec’]) Output DVDs Yearly Sales:Continue reading “Python Chain Map”
Named tuple
from collections import namedtupleimport csv nt = namedtuple(‘Numbers’,[‘a’,’b’,’c’]) numbers = nt(1,2,3) print(‘Total: ‘,numbers.a + numbers.b + numbers.c) days = namedtuple(‘days’,’Sunday Monday Tuesday Wednesday’) print(days._fields) print() Customers = namedtuple(‘Customers’, ‘id, name, type’) #read csv file using named tuplefor customer in map(Customers._make, csv.reader(open(“customers.csv”, ‘r’))): print(customer.id, customer.name,customer.type) Output Total: 6(‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’) id name type101Continue reading “Named tuple”
Syntax error exception
try: employees = eval(input(‘Enter elements in list: ‘)) print(employees) except SyntaxError: print(‘Invalid syntax’) Output Enter elements in list: [4,5,6,7,8)Invalid syntax