CSV is one of the most popular formats to store and transfer textual and numeric data. That is why many modern programming languages provide easy ways to read, process and write such data.
Python is a very easy to learn language and you can write simple programs on it quickly. It also does not require to install many dependencies to start coding on it and is supported on all popular operating systems.
Below we will review Python CSV library which is the most straightforward to quickly read the CSV data in Python.
You can read the CSV file to dictionary in Python by using csv.DictReader function which has the following format:
class csv.DictReader(csvfile, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)
Here "csvfile" is the name of CSV file you want to read. "Dialect" is a type of text file format used to store the data. Default dialect is "excel" which is used for comma delimited CSV files, but you can also specify "excel-tab" to read the files in TSV format or "unix" to process files in UNIX CSV file format. "args" allows to override parsing parameters used by "dialect". For example you can specify newline='' to enable universal newline processing.
"fieldnames" is an important parameter which allows to specify field names to be used as keys in the resulting dictionary. This field can either contain names for all the fields or could be empty. If "fieldnames" field is empty than the first row in CSV will be used to populate the field names. Make sure that you either specify the "fieldnames" parameter or have first row containing correct field names.
Below is an example of how csv.DictReader could be used on real data.
import csv
with open('csvfile.csv', newline='') as csvFile:
reader = csv.DictReader(csvFile)
for row in reader:
print(row)
The code above will open csvfile.csv and output lines from it to the console.
For example if you provide the following csvfile.csv:
Name,Age,Gender
John,30,Male
Melissa,25,Female
Alan,42,Male
Chelsey,40,Female
You will get the following output:
OrderedDict([('Name', 'John'), ('Age', '30'), ('Gender', 'Male')])
OrderedDict([('Name', 'Melissa'), ('Age', '25'), ('Gender', 'Female')])
OrderedDict([('Name', 'Alan'), ('Age', '42'), ('Gender', 'Male')])
OrderedDict([('Name', 'Chelsey'), ('Age', '40'), ('Gender', 'Female')])
CSV Quick Info | |
---|---|
Comma Separaed Values | |
MIME Type | |
text/csv | |
Opens with | |
Microsoft Excel | |
Apple Numbers | |
Google Sheets | |
Microsoft Office Online |