python1 Min Read

Python Code - How To Read CSV into an Array of Arrays

Gorav Singal

October 05, 2021

TL;DR

Use Python's csv.reader() to read a CSV file and store each row as a list within a list, creating an array of arrays data structure.

Python Code - How To Read CSV into an Array of Arrays

Introduction

In last post, we saw How to read CSV with Headers into Dictionary

In this post, we will read the csv data into array of arrays.

Lets assume we have a csv something similar to following:

firstname,lastname,address,city,state,pin
John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123

Python code to read CSV into Array of Dictionary

def read(filepath):
  content = []
  with open(filepath) as csvfile:
    csv_reader = csv.reader(csvfile)

    for row in csv_reader:
      content.append(row)

  return content

content = read("addresses.csv")
print(content)

The Output as Array of Dictionary

[
  ['John', 'Doe', '120 jefferson st.', 'Riverside', ' NJ', ' 08075'],
  ['Jack', 'McGinnis', '220 hobo Av.', 'Phila', ' PA', '09119'],
  ['John "Da Man"', 'Repici', '120 Jefferson St.', 'Riverside', ' NJ', '08075'],
  ['Stephen', 'Tyler', '7452 Terrace "At the Plaza" road', 'SomeTown', 'SD', ' 91234'],
  ['', 'Blankman', '', 'SomeTown', ' SD', ' 00298'],
  ['Joan "the bone", Anne', 'Jet', '9th, at Terrace plc', 'Desert City', 'CO', '00123']
]

Hope it helps.

Share

Related Posts

Python Code - How To Read CSV with Headers into an Array of Dictionary

Python Code - How To Read CSV with Headers into an Array of Dictionary

Introduction Lets assume we have a csv something similar to following: Python…

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Introduction We often require to execute in timed manner, i.e. to specify a max…

How to Solve Circular Import Error in Python

How to Solve Circular Import Error in Python

Introduction To give some context, I have two python files. (Both in same folder…

Python - Some useful Pytest Commands

Python - Some useful Pytest Commands

Introduction In this post, we will explore some useful command line options for…

Latest Posts

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…

Security Ticketing and Incident Response

Security Ticketing and Incident Response

The worst time to figure out your incident response process is during an…

Security Mindset for Engineers — Think Like an Attacker

Security Mindset for Engineers — Think Like an Attacker

Most engineers think about security the way they think about flossing — they…

Secrets Management — Vault, SSM, and Secrets Manager

Secrets Management — Vault, SSM, and Secrets Manager

I’ve watched a production database get wiped because someone committed a root…

Penetration Testing Basics for Developers

Penetration Testing Basics for Developers

Most developers think of penetration testing as something a separate security…

OWASP Top 10 for Cloud Applications

OWASP Top 10 for Cloud Applications

The OWASP Top 10 was written for traditional web applications. But in the cloud…