Jan 9, 2013
Jan 2, 2013
Python Application 4 - Replace Text Content
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 22 - Regular Expression 5 \n------------------------------------\n"
import re
in_str = raw_input("Enter a text : ")
se_str = raw_input("Enter search string : ")
re_str = raw_input("Enter replace string : ")
count = 10
reg1 = re.compile(se_str)
print "Result : ",reg1.sub(re_str,in_str,count)
print "\n-----------------------------------\n"
#END
Python Application 3 - Check Email Address
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
in_str = raw_input("Enter an email address : ")
se_str = "\w+@\w+\.[com|in|org|net]"
reg1 = re.compile(se_str)
search1 = reg1.search(in_str)
else:
print "\nInvalid email address"
print "\n-----------------------------------\n"
#END
print "\n------------------------------------\nChapter 20 - Regular Expression 4 \n------------------------------------\n"
import rein_str = raw_input("Enter an email address : ")
se_str = "\w+@\w+\.[com|in|org|net]"
reg1 = re.compile(se_str)
search1 = reg1.search(in_str)
if search1:
print "\nValid email address"else:
print "\nInvalid email address"
print "\n-----------------------------------\n"
#END
Python Application 2 - Extract File Content
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
f1 = open("logfile","r")
for i in f1.readlines():
j = i.split(' ')
print j
#END
Python Application 1 - Format File Content
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
input_file = raw_input("\nEnter input file name : ")
output_file = "file_new.txt"
h1 = open(input_file,"r")
print "\n----------------------------"
print "Input File Content"
print "----------------------------\n"
for i in h1:
print i,
h1.close()
h1 = open(input_file,"r")
h2 = open(output_file,"w")
k=0
for i in h1:
i2 = i.split(',')
for j in i2:
n = len(j)
if j[n-1] =="\n":
h2.write(j)
else:
h2.write(j)
print"----------------------------\n"
h1 = open(output_file,"r")
for i in h1:
print i,
print "\n---------------------------\n"
#END
input_file = raw_input("\nEnter input file name : ")
output_file = "file_new.txt"
h1 = open(input_file,"r")
print "\n----------------------------"
print "Input File Content"
print "----------------------------\n"
for i in h1:
print i,
h1.close()
h1 = open(input_file,"r")
h2 = open(output_file,"w")
k=0
for i in h1:
i2 = i.split(',')
for j in i2:
n = len(j)
if j[n-1] =="\n":
h2.write(j)
else:
h2.write(j)
h2.write("\n")
h1.close()
h2.close()
print "\n---------------------------"
print "Output File Content - file_new.txt"print"----------------------------\n"
h1 = open(output_file,"r")
for i in h1:
print i,
print "\n---------------------------\n"
#END
Chapter 22 – Python Regular Expression Part 5
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 22 - Regular Expression 5 \n------------------------------------\n"import re
in_str = raw_input("Enter a text : ")
se_str = raw_input("Enter search string : ")
re_str = raw_input("Enter replace string : ")
count = 2
reg1 = re.compile(se_str)
print "Result : ",reg1.sub(re_str,in_str,count)
print "\n-----------------------------------\n"
#ENDChapter 21 – Python Regular Expression Part 4
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Joseprint "\n------------------------------------\nChapter 21 - Regular Expression 4 \n------------------------------------\n"
import re
in_str = raw_input("Enter an email address : ")
se_str = "\w+@\w+\.[com|in|org|net]"
reg1 = re.compile(se_str)
search1 = reg1.search(in_str)
if search1:print "\nValid email address"
else:
print "\nInvalid email address"
print "\n-----------------------------------\n"
#END
Chapter 20 – Python Regular Expression Part 3
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 20 - Regular Expression 3 \n------------------------------------\n"
import re
in_str = raw_input("Enter the text : ")
se_str = raw_input("Enter search string : ")
reg1 = re.compile(se_str)
find1 = reg1.findall(in_str)
print "\nOutput of - .findall() : ", find1
print "\n-----------------------------------\n"
iter1 = reg1.finditer(in_str)
print "Output of - .finditer() : \n"
for i in iter1:
print i.group()
print "\n-----------------------------------\n"
#END
Chapter 19 – Python Regular Expression Part 2
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 19 - Regular Expression 2 \n------------------------------------\n"
import re
match_str = "jijo k 50 JOSE"
search_str = "[a-z]+.*"
print "Input String : %s"%match_str
print "\n-----------------------------------\n"
reg1 = re.compile(search_str,re.IGNORECASE)
match = reg1.match(match_str)
print "Address Location : ",reg1.match(search_str)
print "Search string : ",search_str
print "Matched string : ",match.group()
print "\n-----------------------------------\n"
search_str = '[a-z]+\s[a-z]\s[0-9]+\s\w[A-Z]'
reg1 = re.compile(search_str)
match = reg1.match(match_str)
print "Search string : ",search_str
print "Matched string : ",match.group()
print "\n-----------------------------------\n"
#END
Chapter 18 - Python Regular Expression Part 1
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 18 - Regular Expression 1 \n------------------------------------\n"
import re
match_str = raw_input("Enter a text : ")
search_str = raw_input("Enter search string : ")
reg1 = re.compile(search_str,re.IGNORECASE)
print "Address Location : ",reg1.match(match_str)
print "\n-----------------------------------\n"
match = reg1.match(match_str)
if match:
print "Match - Found : ",match.group()
else:
print "No match found !!! "
print "\n-----------------------------------\n"
search = reg1.search(match_str)
if search:
print "Search Found : ",search.group()
else:
print "No searched result !!!"
print "\n-----------------------------------\n"
#END
Chapter 17 - Python High Level File I/O Operations
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
import shutil
print "\n------------------------------------\nChapter 17 - High level File I/O \n------------------------------------\n"
s = shutil
s.copy("data1","data5")
print "File data1 copied to data5 -shutil.copy() \n"
s.copy2("data1","data6")
print "File data1 copied to data6 without changing timestamp -shutil.copy1() \n"
s.move("data5","temp/data7")
print "File data5 moved to temp/data5 -shutil.move() \n"
srcdir = "temp"
desdir = "temp2"
s.rmtree(desdir)
print "Remove the whole directory tree -shutil.rmtree() \n"
s.copytree(srcdir,desdir)
print "Copy the whole directory tree -shutil.copytree() \n"
print "\n----------------------------------------"
#END
Chapter 16 - Python Functions
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 16 - Functions \n------------------------------------\n"
def sum_mul(a,b):
return a+b,a*b
c,d = sum_mul(10,20)
print "Sum = %d and Product = %d" %(c,d)
print"----------------------------------------"
#END
Chapter 15 - Python Exceptions
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 15 - File Writing \n------------------------------------\n"
while 1:
f1=raw_input("Enter the file name : ")
try:
h1 = open(f1,"r")
print "\nFile contents are :\n",h1.read()
break
except:
print "Problem in opening file : ",f1
print"----------------------------------------"
#END
Jan 1, 2013
Chapter 14 – Python File Write Operations
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 14 - File Writing \n------------------------------------\n"
h1 = open("data1","r")
h2 = open("data2","w")
h3 = open("data3","w")
print h1
h1 = open("data1","r")
s = h1.readlines()
print "\nReading the Content - readlines() : ",s
for i in s:
h2.write(i)
print "File contents are written to data2 using write()"
h3.writelines(s)
print "File contents are written to data3 using writelines()"
print "\n--------------------------------------\n"
h1.close()
h2.close()
#END
Chapter 13 - Python File Read Operations
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 13 - File Reading \n------------------------------------\n"
h1 = open("data1","r")
print h1
print "\nReading the first line -readline() : ",h1.readline()
h1 = open("data1","r")
print "\nReading the full content - read() : \n",h1.read()
h1 = open("data1","r")
print "\nReading the First 25 Character - read(25) : ",h1.read(25)
h1 = open("data1","r")
print "\nReading the Content - readlines() : ",h1.readlines()
print "\n--------------------------------------\n"
#END
Chapter 12 - Python While Loop
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 12 - While Loop \n------------------------------------\n"
c=1
while(c!=0):
i = input("Enter an integer : ")
while(i > 0):
print i
i -= 1
c = input("To quit press 0 : ")
print "\n--------------------------------------\n"
#END
Chapter 11 - Python For Loop
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 11 - For Loop \n------------------------------------\n"
for i in range(10,25,2):
print i
print "\n--------------------------------------\n"
#END
Chapter 10 - Python Command Line Arguments
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 10 - Command Line Parameters \n------------------------------------\n"
import sys
if(len(sys.argv) < 3):
print "You should enter 2 argument"
elif(len(sys.argv) >=3):
print "You entered %d argument " %(len(sys.argv)-1)
print "The sys.argv values are : ", sys.argv
print "sys.argv values in separte lines are...."
for i in sys.argv:
print i
print "\n--------------------------------------\n"
#END
Chapter 9 - Python Conditionals
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 9 - Conditionals \n------------------------------------\n"
a = input("Enter integer A : ")
b = input("Enter integer B : ")
if a == b:
print "A and B are same"
elif a < b:
print "B is larger"
else:
print "A is larger"
print "\n--------------------------------------\n"
#END
Chapter 8 - Python Dictionary Datatypes
Step 1
Open any text editor (gedit,pico,vi,nano) then enter the following python script and save the file with .py extension.
#!/usr/bin/python
#Author : Jijo K Jose
print "\n------------------------------------\nChapter 8 - Dictionaries\n------------------------------------\n"
name = {'fname':'Jijo','mname':'K','lname':'Jose'}
print "\n", type(name)
print "\nDictonary Values : ", name
print "\nname['mname'] : " + name['mname']
print "\nAll keys -keys() : ", name.keys()
print "\nAll values -values() : ", name.values()
del name['mname']
print "\nDelete value del name['key'] : ", name
print "\nIterate through for loop items... -name.iteritems()\n"
for k,v in name.iteritems():
print k + "---->" + v
name['fname']= ['Jijo','Jibin','Jijan']
name['mname']= 'K'
name['lname']= 'Jose'
print "\nAdd more value to same key -name['fname']='value' : ", name
print "\n------------------------------------\n"
#END
Subscribe to:
Posts (Atom)