A CSV file (Comma Separated Values file) is a type of plain text file that uses specific structuring to arrange tabular data. Because it's a plain text file, it can contain only actual text data—in other words, printable ASCII or Unicode characters .
When time comes for you to use a Csv file or a spreadsheet in your program. This article will be of great help as you will be able to open and work with csv files, know what modules are used for csv files, modify and work with csv as if they were they were yours
Let's get Started
!!!
Python as a high level programming has a pre built module for csv files which is called "csv".
Below are the steps for using Python pre built module to analyse csv files
-
Importing the Csv Module
Since the module is already prebuilt you will only import else you will need to installimport csv
Opening the csv file using the with statement.
The with statement is preferred by most Python developers because it automates the close() function after using its open().
The with statement
with open ("notes.csv") as csv_file:
data=csv.reader(csv_file)
- open() : is a python function used to open files in a main.py
- csv_file : is a variable which stores the file that is been open
- data : is a variable assigned to the information extracted from the csv_file after its been accessed by the csv module.
- .reader() : is a csv function for reading the content of a opened csv file.
N:B the reader() is read only. If you print data variable at this stage, it will generate numbers and code which represents the location of the csv file on the computer ROM.
**
Converting a csv file to List and Dictionary**
After the file is been read by the csv module. You could start making your modifications. You can convert it to either a list or dictionary. We will walk you through the two conversions.
1). Converting csv file to a list.
a. Create an empty stored in a variable.
List = [].
b. Loop through the data( variable which you use to store the content of the file when the reader() was used).
c. Append each item in the data to your new list.
Your codes should be in the below format
When the above code is run. It will outpu the list of items in the csv file beginning from the first row and convert each row to a list type object. It will output the below
With this, you can work with your csv files like you do with your list objects.
2). Converting a csv file to Dictionary.
A dictionary works with its keys and values.
To convert a csv file into a dictionary, we will need a key and value.
In this article, we will use an item in the csv_file as the key and the other as its value since it is of two columns.
a). Create an empty dictionary and assign it to a variable.
b). Loop through the data
c). Add the key and value to the empty dictionary.
Your codes should be in the below format:
The key used in the above is the item at index 0 on each row of the Csv file and its value is that at index 1.
The following would be its output
Also with the above out you can also treat your Csv file like that of a dictionary. For example you could modify its value, its key, change the values and keys, and other dictionary features.
Now let's advance a little.
How would it be if we could make all letters in the csv file become a value to a key called Letters and do likewise to the words too? It sound nice right?.
Steps
A). Create a new empty list for the letters.
B). Use a for loop to append all the letters to the new list excluding the "letter" string.
It should be like the below
import csv
> list = []
with open("Notes.csv") as csv_file:
data = csv.reader(csv_file)
for each in data:
list.append(each)
Letters = []
for Char in list:
If char[0] != "letters":
Letters.append(char[0]
The if statement in the above is used to exclude the letter string from the list
Char = a loop variable
Char[0] = the item at index 0 of Char variable which is the letter in csv file.
C). Create another empty list with a variable
words.
D). Use a for loop to append all the word to the words list excluding the "words" string.
Your codes should be like below
import csv
list = []
with open("Notes.csv") as
data_file:
data = csv.reader(data_file)
for each in data:
list.append(each)
Letters = []
for char in list:
if char[0] != "letters":
Letters.append(char[0]
Words = []
for word in list:
if word[0] != "words":
Words.append(word[0])
E). Create an empty dictionary called dict.
F). Add a key called Letters to the empty dict and makes it value to be the Letters list object
G). Add another key called Words to the dict and makes it value to be words list object.
Your code should be like the following
Following the above steps, you have completed the task. If you print the dict object you will get what task we want.
It's result will be:
The letters are now the value of Letters and the words is now the value of Words.
It is very obvious that it took many lines of code to carry out a mini task using the python csv module.
Another module which python programmers use when the come across csv files is a module called Pandas.
Pandas is a software library written for the Python programming language for data manipulation and analysis. In particular, it offers data structures and operations for manipulating numerical tables and time series
In our next article, we will explain more about pandas
Kindly like, comment and follow !!!!!
Top comments (0)