Python: Load / Read Multiline CSV File

access_time 9 months ago visibility2165 comment 0

CSV is a common data format used in many applications. It's also a common task for data workers to read and parse CSV and then save it into another storage such as RDBMS (Teradata, SQL Server, MySQL). In my previous article PySpark Read Multiple Lines Records from CSV I demonstrated how to use PySpark to read CSV as a data frame. This article will show you several approaches to read CSV files directly using Python (without Spark APIs).

CSV data file

The CSV file I'm going to load is the same as the one in the previous example. The file is named as data.csv with the following content:

1,Record 1,Hello World!
2,Record 2,Hello Hadoop!
3,Record 3,"Hello 
4,Record 4,Hello!

There are 4 records and three columns. One record's content is across multiple line. 


All the following code snippets runs on a Windows 10 machine with Python 3.8.2 64bit. It should work on other platforms but I have not tested it. Please bear this in mind. 

Use built-in csv module

csv module can be used to read CSV files directly. It can be used to both read and write CSV files. 

Refer to official docs about this module. 

Sample code

import csv

file_path = 'data.csv'

with open(file_path, newline='', encoding='utf-8') as f:
    reader = csv.reader(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    for row in reader:

The above code snippet reads CSV with all default options and it can handle multi-line CSV automatically.

The output looks like this:

['ID', 'Text1', 'Text2']
['1', 'Record 1', 'Hello World!']
['2', 'Record 2', 'Hello Hadoop!']
['3', 'Record 3', 'Hello \r\nKontext!']
['4', 'Record 4', 'Hello!']

Use Pandas

Pandas has API to read CSV file as a data frame directly.

Read this document for all the parameters: pandas.read_csv.

Sample code

import pandas as pd
file_path = 'data.csv'
pdf = pd.read_csv(file_path)

For the sample CSV files, by default it can handle it properly. If your CSV structure/content is different, you can customize the API call.

The output looks like the following:

   ID     Text1               Text2
0   1  Record 1        Hello World!
1   2  Record 2       Hello Hadoop!
2   3  Record 3  Hello \r\nKontext!
3   4  Record 4              Hello!

For Pandas dataframe, you can also write the results into a database directly via to_sql function.

info Last modified by Raymond 2 months ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn or Twitter.

Want to publish your article on Kontext?

Learn more

More from Kontext

visibility 1471
thumb_up 0
access_time 2 years ago

This code snippet shows how to convert string to date.

visibility 1838
thumb_up 0
access_time 2 years ago

In one of my previous articles about Password Security Solution for Sqoop , I mentioned creating credential using hadoop credential command. The credentials are stored in JavaKeyStoreProvider. Credential providers are used to separate the use of sensitive tokens, secrets and passwords from the ...

Pandas DataFrame Plot - Pie Chart
visibility 9767
thumb_up 0
access_time 10 months ago

This article provides examples about plotting pie chart using  pandas.DataFrame.plot  function. The data I'm going to use is the same as the other article  Pandas DataFrame Plot - Bar Chart . I'm also using Jupyter Notebook to plot them. The DataFrame has 9 records: DATE TYPE ...