Open CSV File

Information, tips and instructions

Read CSV File on Python as an Array

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 in Python by using csv.reader function which has the following format:

csv.reader(csvfile, dialect='excel', **fmtparams)

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. "fmtparams" allows to override parsing parameters used by "dialect". For example you can specify newline='' to enable universal newline processing.

Below is an example of python code to read CSV file.

import csv

with open('csvfile.csv', newline='') as csvFile:
reader = csv.reader(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:

['Name', 'Age', 'Gender']
['John', '30', 'Male']
['Melissa', '25', 'Female']
['Alan', '42', 'Male']
['Chelsey', '40', 'Female']