import shelve
# with shelve.open("D:/WORK/2018/February/22-02-2018-Thursday/shelvetest1")as fruit:
# fruit = {"orange":"its a fruit which is orange in color",
# "apple":"good for health"}
# print(fruit)
# print(fruit["orange"])
# print("="*40)
# print(fruit["orange"])
# print(fruit)
with shelve.open("D:/WORK/2018/February/22-02-2018-Thursday/shelvetest2")as fruit:
fruit["orange"]="its a fruit which is orange in color"
fruit["apple"]="good for health"
print(fruit)
print("="*40)
#the below peice of code will throw an error
# print(fruit["orange"])
with shelve.open("D:/WORK/2018/February/22-02-2018-Thursday/shelvebike1")as bike:
bike["name"]="tvs"
bike["engine_cc"]="250 cc"
#even if removed and executes the below code it will be saved
#in the first run and remains there,which is not the case if
#it is normal dictionary
# bike["engin_cc"]="250 cc"
bike["model"]="victor"
print(bike)
for i in bike:
print(i)
print(bike[i])
###############################################################################
import shelve
with shelve.open("D:/WORK/2018/February/22-02-2018-Thursday/shelvebike1")as bike:
bike["name"]="tvs"
bike["engine_cc"]="250 cc"
#even if removed and executes the below code it will be saved
#in the first run and remains there,which is not the case if
#it is normal dictionary
# bike["engin_cc"]="250 cc"
#we can delete that key using,it has to be run one time only
# del bike["engin_cc"]
bike["model"]="victor"
print(bike)
for i in bike:
print(i)
print(bike[i])
Thursday, 22 February 2018
Python Shelve- Day 12(1 day later)
Python Read Write File,Binary Read Write File - Day 11(2 days later)
# #this shows how to open and read contents from the file
# samplefilevar = open("D:/WORK/2018\February/20-02-2018-Tuesday/pythonsamplereadfile.txt",'r')
# for linevar in samplefilevar:
# print (linevar)
# samplefilevar.close()
#
# print("===="*20)
# #print only the lines containing the specific word
# samplefilevar = open("D:/WORK/2018\February/20-02-2018-Tuesday/pythonsamplereadfile.txt",'r')
# for linevar in samplefilevar:
# if "file" in linevar.lower():
# print (linevar,end = '')
# samplefilevar.close()
# print("===="*20)
# #using with keyword will automatically close the file,we dont need to
# #use the separate close statement
#
# with open("D:/WORK/2018\February/20-02-2018-Tuesday/pythonsamplereadfile.txt",'r')as var2:
# for line in var2:
# #end = '' is used to avoid using the extra empty line
# print(line,end='')
# with open("D:/WORK/2018\February/20-02-2018-Tuesday/pythonsamplereadfile.txt",'r')as var2:
# line =var2.readline()
# print(line)
# with open("D:/WORK/2018\February/20-02-2018-Tuesday/pythonsamplereadfile.txt",'r')as var2:
# #readlines will read all lines and piut it into list,so it is better to use readline,
# #since the readlines will read the entire file in memory,where as readline will
# #go row by row
# lines =var2.readlines()
# print(lines)
#
# for line1 in lines:
# print(line1,end='')
#write contents to a file
# cities = ["Delhi","Bombay","Chennai"]
#
# with open("D:/WORK/2018\February/20-02-2018-Tuesday/pythonsamplewritefile1.txt",'w')as var2:
# for city in cities:
# print(city,file=var2)
#
# with open("D:/WORK/2018\February/20-02-2018-Tuesday/pythonsamplewritefile1.txt",'r')as var3:
# cities = var3.readlines()
# print(cities)
#
# #strip functions strips the character that is present at the end or beginning
#,but not the middle
# for city in cities:
# print(city)
# print(city.strip("\n"))
#
# cityvar1 = []
# with open("D:/WORK/2018\February/20-02-2018-Tuesday/pythonsamplewritefile1.txt",'r')as var3:
# cities = var3.readlines()
# for city in cities:
# cityvar1.append(city)
#
# print(cityvar1)
#
# cityvar2 = []
# with open("D:/WORK/2018\February/20-02-2018-Tuesday/pythonsamplewritefile1.txt",'r')as var3:
# cities = var3.readlines()
# for city in cities:
# cityvar2.append(city.strip("\n"))
#
# print(cityvar2)
#
# strvar1 = "testing"
# print(strvar1.strip("s"))
# print(strvar1.strip("g"))
# #below will remove t at the beginning not the st at the middle
# print(strvar1.strip("st"))
#tuples to file
# albumtuplevar2 = "muthu","rahman",2000,(1,"oruvan oruvan"),(2,"kuluvall"),(3,"vidukathaya")
# print(albumtuplevar2)
# title,composer,year,track1,track2,track3 =albumtuplevar2
# print(title)
# print(composer)
# print(year)
# print(track1)
# print(track2)
# print(track3)
#
# with open("D:/WORK/2018\February/20-02-2018-Tuesday/pythonsamplewritefile2.txt",'w')as var2:
# print(albumtuplevar2,file=var2)
with open("D:/WORK/2018\February/20-02-2018-Tuesday/pythonsamplewritefile2.txt",'r')as var3:
var4 = var3.readlines()
print(var4)
print(var4)
######################################################################
#appending the tables to the existing file
#if we use the mode 'a',it will append it ,if we use the mode 'w',it will overwrite the file
#our challenge is to print like
#1 times 2 is 2
#2 times 2 is 4
#....
#likewise till 13 table
with open("D:/WORK/2018/February/20-02-2018-Tuesday/pythonwritefile3.txt",'w')as tables:
for i in range(1,1000):
for j in range(1,1000):
print("{1} times {0} is {2}".format(i,j,i*j),file=tables)
print("=="*20,file=tables)
#######################################################################
#We can use pickle ,instead of converting the binaty manually and reconverting
#while reading
import pickle
imedla = ('More Mayhem',
'Imelda May',
'2011',
((1,'Pulling the rug'),
(2,'Psycho'),
(3,'Mayhem'),
(4,'Kentish Town Waltz')))
# with open("D:/WORK/2018/February/22-02-2018-Thursday/pythonwritefilebinary.pickle","wb")
as pickle_file:
# pickle.dump(imedla,pickle_file)
with open("D:/WORK/2018/February/22-02-2018-Thursday/pythonwritefilebinary.pickle","rb")
as imelda_pickled:
imelda2 = pickle.load(imelda_pickled)
print(imelda_pickled)
print(imelda2)
album,artist,year,tracklist = imelda2
print (album)
print (artist)
print (year)
for track in tracklist:
trackno,tracktile = track
print("{0} : {1}".format(trackno,tracktile))
#########################################################################################
#storing multiple objects in the binary file and retrieving it in the same order
import pickle
imedla = ('More Mayhem',
'Imelda May',
'2011',
((1,'Pulling the rug'),
(2,'Psycho'),
(3,'Mayhem'),
(4,'Kentish Town Waltz')))
even = list(range(0,10,2))
odd = list(range(1,10,2))
with open("D:/WORK/2018/February/22-02-2018-Thursday/pythonwritefilebinary2.pickle","wb")
as pickle_file:
pickle.dump(imedla,pickle_file,protocol=0)
pickle.dump(even,pickle_file,protocol=0)
pickle.dump(odd,pickle_file,protocol=0)
pickle.dump(12345,pickle_file,protocol=0)
#to read the above objects we need to read it in the same order ie..tuples,list,variable
with open("D:/WORK/2018/February/22-02-2018-Thursday/pythonwritefilebinary2.pickle","rb")
as imelda_pickled:
imelda2 = pickle.load(imelda_pickled)
even_list = pickle.load(imelda_pickled)
odd_list = pickle.load(imelda_pickled)
x = pickle.load(imelda_pickled)
print(imelda_pickled)
print(imelda2)
album,artist,year,tracklist = imelda2
print (album)
print (artist)
print (year)
for track in tracklist:
trackno,tracktile = track
print("{0} : {1}".format(trackno,tracktile))
print("="*40)
for i in even_list:
print(i)
print("="*40)
for i in odd_list:
print(i)
print("="*40)
print(x)
###################################################################################
Subscribe to:
Posts (Atom)