Page 201 - Ai_V3.0_c11_flipbook
P. 201

CSV Files

                 A CSV (Comma-Separated Values) file is a popular and widely used format for storing and exchanging tabular data.
                 It serves as a lightweight and versatile means of organising data into rows and columns, akin to a spreadsheet or a
                 database table. In its simplest form, each line of a CSV file represents a single record, with individual data fields separated
                 by commas. However, CSV files offer flexibility in delimiters, allowing for the use of alternative separators like semicolons
                 or tabs. Typically, the first row of a CSV file contains headers, defining the names of each column, while subsequent
                 rows hold the corresponding data entries. Due to its text-based nature, CSV files are platform-independent and easily
                 readable by both humans and machines. This simplicity, combined with its compatibility with various programming
                 languages and tools, makes CSV a go-to choice for data interchange, storage, and analysis across diverse domains such
                 as finance, research, and software development. There are various ways by which you can create a CSV file, such as Excel
                 (by saving a spreadsheet with .csv extension).

                 For example, the CSV file contains the following data:
                     Customer_ID, First_Name, Last_Name, City
                     1, Akash, Patel, Mumbai
                     2, Priya, Sharma, Delhi
                     3, Aarav, Singh, Jaipur
                     4, Neha, Trivedi, Ahmedabad
                     5, Rahul, Mehta, Bangalore
                 Some basic operations of CSV files are as follows:

                  • •    Importing the CSV library: In Python, the csv module provides functionality to work with CSV files. It includes
                     classes to read and write tabular data in CSV format.

                         import csv
                  • •    Opening a CSV file in reading mode: When opening a file in reading mode (‘r’), you are telling Python that you
                     only intend to read from the file, not modify it. The csv.reader() function then reads the contents of the file line
                     by line. Note that, you have already created CSV file that you want to open. For example, Customer.csv.

                      Program 25: To opening a CSV file in reading mode

                     import csv
                     with open('Customer.csv', 'r') as file:
                         reader = csv.reader(file)
                         for row in reader:

                             # display data
                  • •    Opening a CSV File in writing mode: When opening a file in writing mode (‘w’), you are telling Python that you
                     intend to write to the file. If the file already exists, it will be truncated (emptied) first. If it doesn’t exist, a new file will
                     be created.
                      Program 26: To opening a CSV file in writing mode

                     import csv
                     with open('Customer.csv', 'w', newline='') as file:
                          writer = csv.writer(file)

                          # Write rows here
                  • •    Closing a CSV file: In Python, it is important to close files after you have finished working with them. However,
                     using the with statement automatically closes the file when the block is exited, so you don’t have to worry about
                     explicitly closing it.

                                                                                         Python Programming     199
   196   197   198   199   200   201   202   203   204   205   206