File Input Output and
Sequence Processing
Functions
© Gaurav Dixit Slide 1 of 27
Objectives
By the end of this session, you should be able to understand:
• File operations
• Comprehension tools like : zip, map, filter and lambda
© Gaurav Dixit Slide 2 of 27
FILES
File opening modes:
r : read mode w : write mode a : append mode
f=open(“abc.txt”, “r”) # opens in read mode
• “w” and “a” modes would allow writing to the file
• in “w” mode, an existing file would be truncated
• in “a” mode, the writing would happen at end of an existing file
• “a” mode would create the file if it was not existing
© Gaurav Dixit Slide 3 of 27
FILES
File opening modes for BINARY files:
rb : read mode wb : write mode ab : append mode
• We understand files as either text files or binary files
• Text files are usual files that you can open and read in a text editor
• Binary files contain bytes structured into records as per the application
• Examples of binary files : pdf, doc, xls etc.
© Gaurav Dixit Slide 4 of 27
FILES
• In addition to the opening mode, you can append a ‘+’ symbol.
r+ rb+ : Open in read mode and you can also update the contents
Note: After some content has been read from a file, any write command
would update content at the place where the file reference is at that
point in time. So to update an information that has been read, you may
have to reposition the file reference location so that the update
happens at the correct location.
© Gaurav Dixit Slide 5 of 27
FILES
fo.write(s) # Writes the string s to the file
fo.writelines(L) # Writes the list L to the file
© Gaurav Dixit Slide 6 of 27
FILES
fo.tell() # Gives current offset
fo.seek(offset) # Go to offset (starting from beginning)
fo.seek(offset, 0) # Same, go to offset from beginning
fo.seek(offset, 1) # Go to offset from current location
fo.seek(offset, 2) # Go to offset from end
© Gaurav Dixit Slide 7 of 27
FILES - Demo
Problem :
Read a text file and convert it to upper case and write to a second file.
© Gaurav Dixit Slide 8 of 27
Comprehension tools like : zip,
map, filter and lambda
zip
• Iterates over multiple iterables in parallel
• Returns tuples containing ith element of each iterable
• Returned tuple elements are in order of the iterables
© Gaurav Dixit Slide 10 of 27
zip
L1=[1,2,3]
L2=[5,6,7]
for x in zip( L1, L2 ):
print(x)
# displays (1,5) (2,6) (3,7) each tuple on separate line
© Gaurav Dixit Slide 11 of 27
zip
• zip returns an iterable
• Returned list contains tuples
• Tuple elements are in order of the input iterables
• Length of each tuple is equal to the number of input iterables
• Returned iterable is of length of the shortest input iterables
© Gaurav Dixit Slide 12 of 27
zip
Use of zip in comprehension:
nList=[“Anurag”, “Deepti”, “Jack”, “Hina”]
mList=[ 56, 78, 63, 82]
D={ name:mark for name, mark in zip( nList, mList ) }
© Gaurav Dixit Slide 13 of 27
map()
• Applies a function to every element of input iterable(s)
• Results an iterable
• Function should take same number of arguments as the number of
input iterable(s)
© Gaurav Dixit Slide 14 of 27
map()
def sum(a,b):
return a+b
L1=[1,2,3]
L2=[5,6,7]
L=[ x for x in map( sum, L1, L2) ]
print(L)
© Gaurav Dixit Slide 15 of 27
map()
• map runs for the longest iterable
• if any iterable finishes earlier, None is used as the value
© Gaurav Dixit Slide 16 of 27
filter ( )
• Returns an iterable
• Takes an expression returning True or False
• Applies this expression to input iterable
• Selects elements that return True with the expression
© Gaurav Dixit Slide 17 of 27
filter ( )
def even(a):
if a%2==0: return True
else: return False
L1=[1,2,3,8,6,11,7]
L=[ x for x in filter( even, L1 ) ] # even is the function name
print( L ) # selects even values from a list
© Gaurav Dixit Slide 18 of 27
lambda ( )
• Similar to inline functions of C
• Also called anonymous functions in python
© Gaurav Dixit Slide 19 of 27
lambda ( )
lambda x : x * 5
• This expression takes one argument x.
• Result of the expression is x * 5.
• So if you call this expression as follows:
( lambda x : x * 5 ) ( 7 )
This would result in 35.
• So the lambda expression is exhibiting properties of a function.
© Gaurav Dixit Slide 20 of 27
lambda ( )
square=lambda x : x * x
cube=lambda x : x * x * x
print( square(5) ) # prints 25
print( cube(3) ) # prints 27
© Gaurav Dixit Slide 21 of 27
lambda ( )
• Lambda finds a lot of use in map/filter applications
For instance you wanted to create a list by comprehension, that
contains the sum of corresponding elements of two lists.
© Gaurav Dixit Slide 22 of 27
lambda ( )
def sum(a,b):
return a+b
L1=[1,2,3]
L2=[5,6,7]
L=[x for x in map( sum, L1, L2 ) ]
print(L)
© Gaurav Dixit Slide 23 of 27
lambda ( )
• You could compact your code by replacing the map function with a
lambda, thus eliminating the need of the sum function.
L1=[1,2,3]
L2=[5,6,7]
L=[ x for x in map( lambda a,b : a+b, L1 , L2 ) ]
print( L )
• This also reduces the overhead of function calls happening earlier.
© Gaurav Dixit Slide 24 of 27
lambda ( ) - Exercise
def even(a):
if a%2==0: return True
else: return False
L1=[1,2,3,8,6,11,7]
L=[x for x in filter( even, L1 ) ]
print(L)
# eliminate even function by using lambda
© Gaurav Dixit Slide 25 of 27
Lab Exercises
1. Consider a file containing temperature data one reading per line.
a) Load this file into a list as float numbers.
b) Do this through comprehension.
c) Filter the contents of the file based on a condition without
having an interim list ( all values >25 ).
Try to use filter and lambda where possible.
© Gaurav Dixit Slide 26 of 27
Lab Exercises
2. Consider a CSV file made through Excel, containing student data such as roll no,
name, gender and marks obtained, such as:
R010,Anurag,M,78
R012,Disha,F,67
R013,Gurvinder,M,72
Load this information into appropriate data structure so that you can query on
a roll no and retrieve the name, gender or marks.
Further, write a function to get a list of names who have obtained higher than
certain marks.
L=getHigherThan(60)
© Gaurav Dixit Slide 27 of 27
Lab Exercises
3. You have two text files, article.txt and ignore.txt.
Load the words of ignore.txt into a set as the words to be ignored.
Now you have to build a dictionary of words along with their
frequencies as they occur in the article.txt after you have excluded the
ignore words.
The final result should be sorted word list which do not occur in the
ignore word list. And you wish to get their frequency and density.
Density is (frequency/total words) * 100.
© Gaurav Dixit Slide 28 of 27