Writing to a CSV File Utilizing DictWriter
Lets write the next knowledge to our CSV file. It incorporates details about three totally different US states in an inventory with particular person parts 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 proven under.
import csv fields = ['Name', 'Capital City', 'Largest City', 'Population']; with open('state-data.csv', 'a', newline="") as state_file: author = csv.DictWriter(state_file, fields) author.writerows(state_info)
We first outline the fieldnames as an inventory and retailer them in fields
variable. This lets the author
object know what could be the heading of every column within the CSV file. The writerows()
technique will write all of the rows that we provide to it directly to the CSV file. The one situation for writerows()
is that the rows that we need to write are iterable.
Every particular person row itself must be even be an iterable of strings or numbers if we use the author()
perform like we did within the earlier instance. In any other case, every row must be a dictionary that maps fieldnames
to strings or numbers for the DictWriter
class to course of it.
Lets attempt to write the next knowledge 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 issues in regards to the above knowledge. First, our state Florida is lacking some data. Second, the state Georgia has some further data based mostly what we’re storing within the desk. Third, the keys for Georgia state aren’t within the order wherein we now have outlined the fields for our CSV file.
How can we deal with non-standard knowledge like this? The DictWriter
class provides an answer. For lacking keys, we will merely present a default worth utilizing the restval
parameter. It’s set to an empty string by default. Nevertheless, you can even specify a customized worth resembling Unknown. For further keys, you need to use the extrasaction
parameter to inform DictWriter
to disregard these keys. This parameter will increase a ValueError
by default.
Right here is write to all of the rows directly.
import csv fields = ['Name', 'Capital City', 'Largest City', 'Population'] with open('state-data.csv', 'a', newline="") as state_file: author = csv.DictWriter(state_file, fields, restval="Unknown", extrasaction='ignore') author.writerows(state_info)
Our CSV file will seem like this after all of the writing operations:
Conclusion
This tutorial has lined most of what’s required to have the ability to efficiently learn and write to a CSV file utilizing the totally different capabilities and lessons offered by Python. CSV information have been extensively utilized in software program purposes as a result of they’re straightforward to learn and handle and their small dimension makes them comparatively quick to course of and switch.
Be taught Python
Be taught Python with our full python tutorial information, whether or not you are simply getting began otherwise you’re a seasoned coder trying to study new abilities.
This publish has been up to date with contributions from Monty Shokeen. Monty is a full-stack developer who additionally loves to jot down tutorials, and to study new JavaScript libraries.