How to Read and Write CSV Files in Python

Writing to a CSV File Using DictWriter

Lets write the following data to our CSV file. It contains information about three different US states in a list with individual elements being a dictionary.

The code is as shown below.

We first define the fieldnames as a list and store them in fields variable. This lets the writer object know what would be the heading of each column in the CSV file. The writerows() method will write all the rows that we supply to it at once to the CSV file. The only condition for writerows() is that the rows that we want to write are iterable.

Each individual row itself should be also be an iterable of strings or numbers if we use the writer() function like we did in the previous example. Otherwise, each row should be a dictionary that maps fieldnames to strings or numbers for the DictWriter class to process it.

Lets try to write the following data to our CSV file now:

There are two noteworthy things about the above data. First, our state Florida is missing some information. Second, the state Georgia has some extra information based what we are storing in the table. Third, the keys for Georgia state are not in the order in which we have defined the fields for our CSV file.

How can we handle non-standard data like this? The DictWriter class offers a solution. For missing keys, we can simply provide a default value using the restval parameter. It is set to an empty string by default. However, you can also specify a custom value such as Unknown. For extra keys, you can use the extrasaction parameter to tell DictWriter to ignore those keys. This parameter will raise a ValueError by default.

Here is how to write to all the rows at once.

Our CSV file will look like this after all the writing operations:

Stata Data Table After Writing OperationsStata Data Table After Writing OperationsStata Data Table After Writing Operations

Conclusion

This tutorial has covered most of what is required to be able to successfully read and write to a CSV file using the different functions and classes provided by Python. CSV files have been widely used in software applications because they are easy to read and manage and their small size makes them relatively fast to process and transfer.

Learn Python

Learn Python with our complete python tutorial guide, whether you’re just getting started or you’re a seasoned coder looking to learn new skills.

This post has been updated with contributions from Monty Shokeen. Monty is a full-stack developer who also loves to write tutorials, and to learn about new JavaScript libraries.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Esther Vaati

Writing to a CSV File Using DictWriter

Lets write the following data to our CSV file. It contains information about three different US states in a list with individual elements being a dictionary.

The code is as shown below.

We first define the fieldnames as a list and store them in fields variable. This lets the writer object know what would be the heading of each column in the CSV file. The writerows() method will write all the rows that we supply to it at once to the CSV file. The only condition for writerows() is that the rows that we want to write are iterable.

Each individual row itself should be also be an iterable of strings or numbers if we use the writer() function like we did in the previous example. Otherwise, each row should be a dictionary that maps fieldnames to strings or numbers for the DictWriter class to process it.

Lets try to write the following data to our CSV file now:

There are two noteworthy things about the above data. First, our state Florida is missing some information. Second, the state Georgia has some extra information based what we are storing in the table. Third, the keys for Georgia state are not in the order in which we have defined the fields for our CSV file.

How can we handle non-standard data like this? The DictWriter class offers a solution. For missing keys, we can simply provide a default value using the restval parameter. It is set to an empty string by default. However, you can also specify a custom value such as Unknown. For extra keys, you can use the extrasaction parameter to tell DictWriter to ignore those keys. This parameter will raise a ValueError by default.

Here is how to write to all the rows at once.

Our CSV file will look like this after all the writing operations:

Stata Data Table After Writing OperationsStata Data Table After Writing OperationsStata Data Table After Writing Operations

Conclusion

This tutorial has covered most of what is required to be able to successfully read and write to a CSV file using the different functions and classes provided by Python. CSV files have been widely used in software applications because they are easy to read and manage and their small size makes them relatively fast to process and transfer.

Learn Python

Learn Python with our complete python tutorial guide, whether you're just getting started or you're a seasoned coder looking to learn new skills.

This post has been updated with contributions from Monty Shokeen. Monty is a full-stack developer who also loves to write tutorials, and to learn about new JavaScript libraries.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Esther Vaati


Print Share Comment Cite Upload Translate Updates
APA

Esther Vaati | Sciencx (2017-11-06T11:10:32+00:00) How to Read and Write CSV Files in Python. Retrieved from https://www.scien.cx/2017/11/06/how-to-read-and-write-csv-files-in-python/

MLA
" » How to Read and Write CSV Files in Python." Esther Vaati | Sciencx - Monday November 6, 2017, https://www.scien.cx/2017/11/06/how-to-read-and-write-csv-files-in-python/
HARVARD
Esther Vaati | Sciencx Monday November 6, 2017 » How to Read and Write CSV Files in Python., viewed ,<https://www.scien.cx/2017/11/06/how-to-read-and-write-csv-files-in-python/>
VANCOUVER
Esther Vaati | Sciencx - » How to Read and Write CSV Files in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2017/11/06/how-to-read-and-write-csv-files-in-python/
CHICAGO
" » How to Read and Write CSV Files in Python." Esther Vaati | Sciencx - Accessed . https://www.scien.cx/2017/11/06/how-to-read-and-write-csv-files-in-python/
IEEE
" » How to Read and Write CSV Files in Python." Esther Vaati | Sciencx [Online]. Available: https://www.scien.cx/2017/11/06/how-to-read-and-write-csv-files-in-python/. [Accessed: ]
rf:citation
» How to Read and Write CSV Files in Python | Esther Vaati | Sciencx | https://www.scien.cx/2017/11/06/how-to-read-and-write-csv-files-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.