Pre Quiz – Working with Files
Q1 – Name the python module that supports regular expressions.
The python module re supports regular expression. The functions in the re module lets you check if a particular string matches the given regular expression (or if a given regular expression matches a particular string).
Q2 – Describe the function re.match.
Answer: matches a pattern at the start of the string
re.match() matches a re pattern at the start of the string.
Q3 – Describe the function re.search.
Answer: matches a pattern at any position in the string
re.search() matches a re pattern at any position in the string.
Q4 – Analyse and predict the output of the following code.
This function returns the particular subgroup only. In our case, the 2nd subgroup.
Q5 – Analyse and predict the output of the following
Answer: ‘we are humans’
This function returns the entire match
Practise Session – Working with Files
Q1 – Open the le ‘odi.csv’ using DictReader in read mode and store it in the variable reader. (Use single quotes)
Q2 – Fill the code to open the text le data_science in read mode using with statement and to print all the statements within the le.(Use single quotes)/h3>
Post Quiz – Working with Files
Q1 – Select the correct built-in function(s) to read a line of text from standard input, which by default comes from the keyboard
The input() function reads a line entered on a console by an input device such as a keyboard and convert it into a string and returns it
Q2 – “You can read all the lines in the text le using .readlines() function”. State True or False.
The method readlines() returns a list containing all the lines from the le.
Q3 – “We can open a le in “a” mode to append text towards the end of the le”. State True or False.
We can open le in “a” mode to append text towards the end of the le. The le pointer is at the end of the le if the le exists. If the le does not exist, it creates a new le for writing
Q4 – Select the correct option(s) that describe(s) “pickling” in Python.
Answer: It is used for object serialization
Python pickle module is used for serializing and de-serializing a Python object structure.
Any object in Python can be pickled so that it can be saved on disk. Pickle “serializes” the object rst, before writing it to le.
Pickling is a way to convert a python object (list, dict, etc.) into a character stream.
Q5 – “When we use DictReader for reading small csv les, each line in the le is read as dictionary and columns in the data will be available as keys of the dictionary”. State True or False.
DictReader reads each line of a csv le as a dictionary and every column becomes the key of the dictionary
Check Your Understanding_ Attempt_Files
Q1 – We know that DictReader() is used to read data from a CSV. What does this method return ?
List of Dictionaries
Q2 – Match the following
returns a single line from file
returns the entire file
returns a list containing all lines of the file
Q3 – Every line of a text le is terminated with End of Line character. State true or false.
The End of Line(“EOL”) is a combination of the carriage return and line feed characters. It moves the cursor both down to the next line and to the beginning of that line. Every line of a text le is terminated with End of Line character.
Q4 – The process of conversion of Python object hierarchy into a byte stream is called as
and the inverse operation, whereby a byte stream is converted back into an object hierarchy is called as
Q13 – Which of the following statements are true with respect to the with statement ?
Answer: We don’t have to explicitly close the le handler if we open using ‘with statement’, It is possible to open multiple les with a single ‘with statement’, The ‘with statement’ was rst introduced in Python version 2.5
The ‘with statement’ was rst introduced in Python version 2.5. It is possible to open multiple les with a single ‘with statement’. We don’t have to explicitly close the le handler if we open using ‘with statement’.
- Store Student Data
- Read CSV file
- Filter countries Sachin has played against
- Calculate year-wise total number of runs of Sachin