Python read web page source

URL.py import urllib.request with urllib.request.urlopen(‘file:///F:/Demo/test.html’) as page: htmlContent = page.read() print(htmlContent) Output

Python reading csv file

employee.csv Code,Name,Position,Dept,Sal 100,John,Manager,Admin,12000 101,Martin,Clerk,Admin,8000 102,Smith,Supervisor,Production,10000 103,Cathy,Executive,Sales,9000 104,Scarlet,Manager,Finance,9500 105,Justin,Sr Manager,Hr,15000 reademp.py import csv csv.list_dialects()  [‘excel-tab’, ‘excel’] f = open( ’employee.csv’, ‘r’ ) records = csv.reader( f ) for record in records:        print (record[0],record[1],record[2],record[3],record[4]) Output    

Python file handling another example

mon_exp.txt jan,1200 feb,1300 mar,1200 apr,1234 may,1000 jun,1580 jul,1470 aug,1200 sept,1100 oct,1400 nov,1500 dec,1200 monthexp.py fMonths = open(“mon_exp.txt”,”r+”) sum = 0 for month in fMonths.readlines() :        val =month.strip()        month = val.split(“,”)        sum = sum + int (month[1])         if(month[0] == “mar” ) :  Continue reading “Python file handling another example”

Python tuple example

tuple.py Employees = (“John”,”Smith”,”Davis”,”Cathy”,”Jean”,) print(“\nEmployees\n”) print(Employees) Salaries = (5000,4500,5000,4000,4500) print(“\nSalaries\n”) print(Salaries) print(“\nDisplay employee at third position\n”) print(Employees[2]) print(“\nUsing slice…first three employees\n”) print(Employees[0:3]) print(“\nEmployees and Salaries\n”) EmpSal = Employees + Salaries print(EmpSal) print(“\nNegative indexing for displaying element\n”) print(Employees[-2]) Output  

Python dictionary another example

dict3.py Months = {} Months[“Jan”]=31 Months[“Feb”]=28 Months[“Mar”]=31 Months[“Apr”]=30 Months[“May”]=31 Months[“June”]=30 Months[“Jul”]=31 print(“Print Months with days\n”) print(Months) del Months[“Jan”] print(“\nDictionary after deleting Jan entry”) print(Months) print(“\nClear contents of dictionary”) Months.clear() print(Months) Months[“Jan”]=31 Months[“Feb”]=28 Months[“Mar”]=31 Months[“Apr”]=30 Months[“May”]=31 Months[“June”]=30 Months[“Jul”]=31 Mkeys=Months.keys() print(“\nMonths: “, Mkeys) Dvalues=Months.values() print(“\nDays” ,Dvalues) Mon = Months.items()  print(“\nList Months-Days”) for M in Mon:    Continue reading “Python dictionary another example”

Python dictionary and list example

dict1.py Temperature = { ‘Jan’:[9,12,13,14,15,16],              ‘Feb’:[11,12,16,12,13,14.5],              ‘Mar’:[10,12,11,12,10,12.5] } print(“Month wise Temperature”) print(Temperature) print(“\n”)print(“\nListing Month – Temperature”) for month, temp in Temperature.items():           print(“Month %s : %s” % (month,temp))   Output    

Python dictionary example

dict.py Countries = {} Countries[“Usa”]=”Washington DC” Countries[“France”]=”Paris” Countries[“Italy”]=”Rome” Countries[“Japan”]=”Tokyo” Countries[“Uk”]=”London” print() print(“Country – Capital”) print(Countries) print(“\nListing Country – Capital again”)        for country, capital in Countries.items():           print(“Capital of %s : %s” % (country,capital)) print(“\n Country – Currency”) Countries ={“Us”:”Us Dollar”,”Japan”:”Yen”,”Germany”:”Euro”,”France”:”Euro”} print(Countries) Output

Python Set examples

setdemo.py Products = set() Products.add(“Electronic”) Products.add(“Medical”) Products.add(“Cosmetic”) Products.add(“Paper”) Products.add(“Food”) print(“Products Set”) print(Products) OnlineCustomers=set([‘John’,’Michael’,’Peter’,’Henry’,’Jacob’,’Martin’,’James’]) print(“Online Customers”) print(OnlineCustomers) RetailCustomers = set([‘Mathew’,’Jerry’,’Jean’,’Kimberley’,’Sam’,’James’]) print(“Retail Customers”) print(RetailCustomers)   Customers = OnlineCustomers | RetailCustomers print(“All Customers”) print(Customers) CommonCustomers = OnlineCustomers & RetailCustomers print(“Common Customers”) print(CommonCustomers)    Uncommon = OnlineCustomers – RetailCustomers  print(“Uncommon Customers”) print(Uncommon) OneTimeCustomers = set([‘Zack’,’James’,’Michelle’]) print(“One time customers”) print(OneTimeCustomers)Continue reading “Python Set examples”