Select Page
Software Testing

Python CSV File Read Examples

When you use Python libraries, you can craft your test automation scripts quickly and concisely instead of writing lengthy snippets.

Python CSV

CSV stores data in tabular format. Importing CSV files can be achieved at much faster, rate with less memory consumption. In this blog article, we have listed a few Python CSV File Read Examples for your better understanding and reference. We, as a QA company, use large CSV files to verify data quality between source and target. In order to automate data quality checks, we use Python libraries to import CSV files programmatically.

Built-in CSV Library

Python has built-in CSV library. You can read & write a CSV file. You needn’t install additional packages to use this module.

import csv
with open('test_data.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

Read CSV using Pandas

Pandas is a powerful data analysis Python library. Let’s say for instance if you wish to import large CSV files on one go for Pandas. We, as an automation testing company have used Pandas library for our ETL & Data Analytics projects at various occasions and found it noteworthy.

import pandas as pd
test_data = pd.read_csv("test_data.csv")
#Reading First Value from name column
print(test_data["name"][0])

Convert XLSX to CSV

If you want to convert XLSX to CSV in Python programming, you can try out xlsx2csv library. It converts large XLSX file to CSV.

CleverCSV

CleverCSV detects the dialect of a file automatically , additionally it has command line implementation, if you would like to check the file outside Python environment.

# importing this way makes it easy to port existing code to CleverCSV!
import clevercsv as csv
with open("data.csv", "r", newline="") as fp:
  # you can use verbose=True to see what CleverCSV does:
  dialect = csv.Sniffer().sniff(fid.read(), verbose=False)
  fp.seek(0)
  reader = csv.reader(fp, dialect)
  rows = list(reader)

In Conclusion

When you use Python libraries, you can craft your test automation scripts quickly and concisely instead of writing lengthy snippets. The above listed libraries will be helpful when you handle CSV files. We, as a software testing company, use various Python packages for ETL & Data Analytics testing projects. There are several other Python CSV File Read packages available in the market, However, they are not active in development. We hope you enjoyed this blog article and found it worthwhile.

Comments(0)

Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility