CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. It is one of the most common methods for exchanging data between applications and popular data format used in Data Science. It is supported by a wide range of applications. A CSV file stores tabular data in which each data field is separated by a delimiter(comma in most cases). To represent a CSV file, it must be saved with the .csv file extension.
Reading from CSV file
Python contains a module called
csv for the handling of CSV files. The reader class from the module is used for reading data from a CSV file. At first, the CSV file is opened using the open() method in ‘r’ mode(specifies read mode while opening a file) which returns the file object then it is read by using the
reader() method of CSV module that returns the reader object that iterates throughout the lines in the specified CSV document.
csv.reader(csvfile, dialect='excel', **fmtparams
Note: The ‘with‘ keyword is used along with the
open() method as it simplifies exception handling and automatically closes the CSV file.
Consider the below CSV file –
[['Steve', 13, 'A'], ['John', 14, 'F'], ['Nancy', 14, 'C'], ['Ravi', 13, 'B']]
Writing to CSV file
csv.writer class is used to insert data to the CSV file. This class returns a writer object which is responsible for converting the user’s data into a delimited string. A CSV file object should be opened with newline=” otherwise, newline characters inside the quoted fields will not be interpreted correctly.
csv.writer(csvfile, dialect='excel', **fmtparams)
csv.writer class provides two methods for writing to CSV. They are
- writerow(): This method writes a single row at a time. Field row can be written using this method.
- writerows(): This method is used to write multiple rows at a time. This can be used to write rows list.
We can also write dictionary to the CSV file. For this the CSV module provides the csv.DictWriter class. This class returns a writer object which maps dictionaries onto output rows.
csv.DictWriter(csvfile, fieldnames, restval=”, extrasaction=’raise’, dialect=’excel’, *args, **kwds)
csv.DictWriter provides two methods for writing to CSV. They are:
writeheader()method simply writes the first row of your csv file using the pre-specified fieldnames.
writerowsmethod simply writes all the rows but in each row, it writes only the values(not keys).
- Writing CSV files in Python
- Reading CSV files in Python
- Reading and Writing to text files in Python
- Reading and Writing XML Files in Python
- Python program to read CSV without CSV module
- Writing data from a Python List to CSV row-wise
- Reading and Writing JSON to a File in Python
- Working with csv files in Python
- How to skip rows while reading csv file using Pandas?
- Writing files in background in Python
- Ways to import CSV files in Google Colab
- Creating a dataframe using CSV files
- Python | Reading .ini Configuration Files
- Saving Text, JSON, and CSV to a File in Python
- Load CSV data into List and Dictionary using Python
- How to Convert an image to NumPy array and saveit to CSV file using Python?
- Python | Read csv using pandas.read_csv()
- Convert CSV to Excel using Pandas in Python
- Convert JSON to CSV in Python
- Convert CSV to HTML Table in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.