DATA FILE HANDLING
What are the different types of Files?
Name of the student is Babitha her class is 12B and her roll number is 12B30.
RNO | NAME | CLASS |
12B30 | BABITHA | 12B |
Table Name: Student
[12B30,’BABITHA’,12B]
INTRODUCTION TO CSV
CSV (comma separated values)
It is used for storing tabular data in a spreadsheet or database
Each line in a csv file is called a record
Each record consists of fields separated by commas(delimiter)
CSV
to and exported from programs that store
data in tables, such as Microsoft Excel or
OpenOffice Calc.
separated values”. Thus, we can say that
a comma-separated file is a delimited text
file that uses a comma to separate values.
Advantages of CSV files
Easier to create
Preferred import and export format for database and spreadsheet
Capable of storing large amount of data
WHY USE CSV?
Thus, in a nutshell, the several advantages that are offered by CSV files are as follows:�
• CSV is faster to handle.
• CSV is smaller in size.
• CSV is easy to generate and import onto a spreadsheet or database.
• CSV is human readable and easy to edit manually.
• CSV is simple to implement and parse.
• CSV is processed by almost all existing applications.
CSV FILE HANDLING IN PYTHON
1. Reading a CSV
2. Writing to a CSV.
3. Searching for a record
4. Deleting a Record
5. Updating a Record
Role of Newline Argument in CSV file
Symbol/ Char | Meaning | Operating System |
CR [\r] | Carriage return | Mac |
LF[\n] | Line Feed | Unix |
CR \LF [ \r \n] | Carriage return Line feed | Ms-DOS, Windows |
Reading from CSV File
Reading from a CSV file is done using the reader object. The CSV file is opened as a text file with Python’s built-in open() function, which returns a file object.
This creates a special type of object to access the CSV file (reader object), using the reader() function.
The reader object is an iterable that gives us access to each line of the CSV file as a list of fields. You can also use next() directly on it to read the next line of the CSV file, or you can treat it like a list in a for loop to read all the lines of the file (as lists of the file’s fields).
PROGRAM TO WRITE INTO A CSV FILE
PROGRAM TO WRITE INTO A CSV FILE
Programs
W.A.F to find the number of records in a CSV file
To avoid the field name : Method 1
To avoid the field name : Method 2
Program to print the records in the form of comma separated values, instead of lists.
csv_reader_object has a method
called line_num that returns the number of lines in our CSV.
line_num is nothing but a counter which returns the number of rows which have been iterated.
Program to search the record of a particular student from CSV file on the basis of inputted name.
Program to print the records in the form of comma separated values, instead of lists.
Program to print the records in the form of comma separated values, instead of lists.
RANDOM ACCESS IN FILES USING TELL() AND SEEK()
seek() can be done in two ways:
f.seek(offset, from_what) #where f is file pointer
For example,
Multiple Choice Questions (MCQs)�
(a) Which of the following is not a valid mode to open a file?
(b) Which statement is used to change the file position to an offset value from the start?
(i) fp.seek(offset, 0) (ii) fp.seek(offset, 1)
(iii) fp.seek(offset, 2) (iv) None of the above
(c) The difference between r+ and w+ modes is expressed as?
(i) No difference
(ii) In r+ mode, the pointer is initially placed at the beginning of the file and the pointer is at the
end for w+
(iii) In w+ mode, the pointer is initially placed at the beginning of the file and the pointer is at the
end for r+
(iv) Depends on the operating system
(d) Which module is used for working with CSV files in Python?
(i) random (ii) statistics (iii) csv (iv) math
(e) Which of the following modes is used for both writing and reading from a binary file?
(i) wb+ (ii) w (iii) wb (iv) w+
(f) Which statement is used to retrieve the current position within the file?
(g) What happens if no arguments are passed to the seek() method?
2. Consider the following code:
f = open("test", "w+")
f.write("0123456789abcdef")
f.seek(-3,2) //Statement 1
print(f.read(2)) //Statement 2
Explain statement 1 and give output of statement 2.
Ans. Statement 1 uses seek() method that can be used to position the file object at a particular place in the
file.
It’s syntax is:
fileobject.seek(offset [, from_what])
So, f.seek(-3,2) positions the fileobject to 3 bytes before end of file.
Output of Statement 2 is:
de
It reads 2 bytes from where the file object is placed.
3. Yogendra intends to position the file pointer to the tenth character from the current position of a text file. Write Python statement for the same assuming "F" is the Fileobject.
4. In which of the following file modes the existing data of the file will not be lost?
rb, ab, w, w+b, a+b, wb, wb+, w+, r+
Ans. In file modes rb, ab, a+b and r+, data will not be lost.
In file modes w, w+b, wb, wb+ and w+, data will be truncated i.e. lost.
5. Write a statement in Python to perform the following operations:
(a) To open a text file "Book.txt" in read mode
(b) To open a binary file "Book.dat" in write mode
Ans. (a) f = open("Book.txt", "r")
(b) f = open("Book.dat", "wb")
6. What is the output of the following code?
fh = open("test.txt", "r")
Size = len(fh.read())
print(fh.read(5))
Ans. No output.
Explanation. The fh.read() of line 2 will read the entire file content and place the file pointer at the end
of file. For the fh.read(5), it will return nothing as there are no bytes to be read from EOF. Thus print()
statement prints nothing.
7. Give the output of the following snippet:
4
8
8. Give the output of the following snippet
Function called words() in python to count no. of words in a text file count.txt
Function called max_word ()in python to show word with maximum length from a text file count.txt