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.
state_info = [ { "Name": "Colorado", "Largest City": "Denver", "Capital City": "Denver", "Population": "5773714" }, { "Name": "Connecticut", "Largest City": "Hartford", "Capital City": "Bridgeport", "Population": "3605944" }, { "Name": "Delaware", "Largest City": "Dover", "Capital City": "Wilmington", "Population": "989948" } ]
The code is as shown below.
import csv fields = ['Name', 'Capital City', 'Largest City', 'Population']; with open('state-data.csv', 'a', newline='') as state_file: writer = csv.DictWriter(state_file, fields) writer.writerows(state_info)
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:
state_info = [ { "Name": "Florida", "Capital City": "Tallahassee" }, { "Name": "Georgia", "Area": "153910", "Largest City": "Atlanta", "Population": "10711908", "Capital City": "Atlanta", } ]
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.
import csv fields = ['Name', 'Capital City', 'Largest City', 'Population'] with open('state-data.csv', 'a', newline='') as state_file: writer = csv.DictWriter(state_file, fields, restval='Unknown', extrasaction='ignore') writer.writerows(state_info)
Our CSV file will look like this after all the 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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.