How to Read CSV File in Python

Posted on

Introduction

In this article, the focus for the content is just on how to read a file. Basically, it has a similar tone with the one available in the previous article. In the previous article exist in this link with the title of ‘How to Read Text File in Python’. That article is focusing on how to read a text file. On the other hand, in this article, it will focus on how to read a CSV file. After using the ‘open()’ method to access the file in a read mode, there is a default method available to read file. It is a method with the name of ‘read()’. But in this context, there is another better method to read CSV file .

How to Read CSV File in Python

Actually, in order to read CSV file, aside from read() method, that method is actually more suitable according to the actual requirement for further process.  That method available is a method with the name of ‘csv.reader’ method. But before the ‘reader’ method which is available in ‘csv’ package exist for utilization, just import the ‘csv’ library or package. Because it is different with the normal ‘read()’ method. It is a method exist in the ‘csv’ package or library. Just define it in the following syntax or pattern :

csv.reader(file, delimiter=",")

In the above method definition, there is an additional parameter or argument. That parameter or argument is the delimiter. That is a limiter for separating in CSV file. Generally, the separator usually exist in the form of comma or semicolon.  As it exist in the previous statement, the ‘reader()’ method is different with ‘read()’ method. The main difference is the existence of additional parameter or argument which is describing delimiter(). This additional parameter or argument is important for further processing step. In order to read the CSV file, the following sequence of steps are the process for achieving it :

  1. As in the first step, create a new file in order to read CSV file. Just create a new file with any name. In this example, it is a file with the name of ‘read_csv.py’. The content of the file exist as follow :

    import csv
    
    file_name = 'myfile.csv'
    file = open(file_name)
    
    data = csv.reader(file, delimiter=",")
    print(data)
    
  2. Next step, just execute that file. But apparently, the output is not presenting the data exist in the file as it is exist in the following output :
    <_csv.reader object at 0x0000029EF8407EF1>
    

    The above output is just printing the memory location of the variable where the object variable of CSV file is being stored. So, the ‘print()’ method does not suitable to achieve the process for printing the data exist inside the CSV file.

  3. In other words, if there is a necessary for retrieving the data from the CSV file, there is a right and suitable process to do that. Just use looping utility available for it. The following execution will print the data will in a list per line. Before going on to the execution process, below is the actual modification of the source code as follows :

    import csv
    
    filename = 'myfile.csv'
    file = open(filename)
    
    data = csv.reader(file, delimiter=",")
    for item in data:
    print(item)
    

    The execution of the above file will present an output which is exist as follows :

    ['NO', 'NAME', 'Score']
    
    ['1', 'Dean', '95']
    

Leave a Reply