CSV Files

A CSV (Comma Separated Values) file is a text-based format that represents the data typically found in a spreadsheet or a database table.

It is a common format for data interchange as it is simple, compact and used everywhere. It will open into Excel with a double click and nearly all databases have a tool to allow import from CSV. It is also readily parseable with simple code.

The basic format is defined by rows of column data. Each row is terminated by a newline to begin the next row. Within the row, each column is distinguished by a comma.

Data within each column can be double quoted to escape embedded commas in the data. Embedded double quotes are escaped with a pair of double quote characters.

CSV files are also called as flat files, as CSV files contain only simple formatted 2 Dimensional tables, Also you donot not sophisticated softwares to handle CSV. You just need a simple text editor to manipulate the CSV files.

CSV With Python Selenium

We have a built-in module for reading CSV files in Python, csv module helps to read csv file in python language.

Sample CSV Files

Please save the below csv content in your local system with .csv extension

CSV file with header


Name,Product,Description
karthiQ,chercher tech,tutorial
larry page,google,search engine
gates,microsoft,operating system			

CSV file without header


karthiQ,chercher tech,tutorial
larry page,google,search engine
gates,microsoft,operating system			

Steps for reading CSV file without Header

  • Import the csv module
  • Open that target .csv file with open() function and store the data
  • using reader() function from csv module, convert the stored data to collection of data. Every row is returned as an array
  • iterate the over the data array that we stored in above step using for loop/or loop of our wish
  • print the value
  • We have read the csv file which has no header

import unittest
import csv

class Test(unittest.TestCase):

    def test_read_csv_file(self):
       with open('D:\\PATH\\file-without-header.csv') as csvDataFile:
        csvReader = csv.reader(csvDataFile)
        for row in csvReader:
            print(row)    
       
if __name__ == "__main__":
    unittest.main()	
csv-file-read-selenium-python

In above example, we have how to read the file with header but sometimes, we may have csv file with a header.

In such cases we have to mark it as header then we have to proceed reading, in this way, we have one more benefit that we can access the value using the headers as keys.

We can also read only required columns from the csv file like below, I have avoided Product column from the csv file.

We will be able to retrieve the columns using their name like row['title'].


import unittest
import csv

class Test(unittest.TestCase):

    def test_read_csv_file(self):
       with open('D:\\PATH\\file-with-header.csv') as csvDataFile:
        csvReader = csv.DictReader(csvDataFile)
        for row in csvReader:
            print(row['Name'], "--", row['Description'])
            
if __name__ == "__main__":
    unittest.main()			


reading-headers-csv-files-selenium-python

Read the urls from the CSV file

In this program, we will learn how to read the url from the csv file and navigate to them in selenium python

What happens in the below program :

  • We have created a function called readCSVFile(), which will read the csv file will return the url and the title as a map.
  • Test method calls this readCSVFile() and gets the content of the file and iterates over the resulting map.
  • Selenium python navigates to the url from array and verifies the given title and actual title are the same using assertEqual() method.

import unittest
import csv
from selenium import webdriver

class Test(unittest.TestCase):
    
    def readCSVFile(self, filename):
       urls = []
       titles = []
    
       with open(filename) as csvDataFile:
           csvReader = csv.DictReader(csvDataFile)
           for row in csvReader:
               urls.append(row['url'])
               titles.append(row['title'])
    
       return dict(zip(urls,titles))

    def test_read_csv_file(self):
        testObject = Test()
        pages=testObject.readCSVFile("D:\\PATH\\file-with-header.csv")
        self.driver = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe') 
        print(pages)
        for url,title in pages.items():
            print(url)
            self.driver.get(url)
            self.assertEqual(title, self.driver.title, "title is not matching")
                    
if __name__ == "__main__":
    unittest.main()

Just like reading CSVs, the csv module provides functions to write data to a CSV file as well. The writer object presents two functions, namely writerow() and writerows().

The difference between them is that the writerow() function will only write one row, and the function writerows() writes several rows at once.


import unittest
import csv

class Test(unittest.TestCase):

    def test_write_csv_file(self):
        myData = [["karthiq", "BE", "Mechanical"], ['Raj', 'BTech', 'Computers']]  
        myFile = open('csv-write-data.csv', 'w')  
        with myFile:  
           writer = csv.writer(myFile)
           writer.writerows(myData)
                    
if __name__ == "__main__":
    unittest.main()


write-csv-file-python-selenium

We can also create a CSV file using dictionaries with help of DictWriter. In the code below, we create a dictionary with the numbers and words.

Then we create a writer object that writes data to our number-values.csv file, which has the set of fields previously defined with the list myHeader.

We write the header row with the writeheader() function, and then the pairs of values using the writerow() function.

Each value's position in the row is specified using the column label.


import unittest
import csv

class Test(unittest.TestCase):

    def test_write_csv_file(self):
        myFile = open('number-values.csv', 'w')  
		with myFile:  
			myNumbers = ['number', 'word']
			writer = csv.DictWriter(myFile, fieldnames=myNumbers)    
			writer.writeheader()
			writer.writerow({'number' : '1', 'word': 'one'})
			writer.writerow({'number' : '3', 'word': 'three'})
                    
if __name__ == "__main__":
    unittest.main()


Read csv file which has '/' as a delimiter


import csv

csv.register_dialect('myDialect', delimiter='/', quoting=csv.QUOTE_NONE)

with open('greetings.csv', newline='') as myFile:  
   reader = csv.reader(myFile, dialect='myDialect')
   for row in reader:
       print(row) 


Write csv file which has '/' as a delimiter


import csv

myData = [[1, 2, 3], ['one', 'two', 'three']]

csv.register_dialect('myDialect', delimiter='/', quoting=csv.QUOTE_NONE)

myFile = open('numbers.csv', 'w')  
with myFile:  
   writer = csv.writer(myFile, dialect='myDialect')
   writer.writerows(myData)


About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group