R
Raymond arrow_drop_down
access_time 4 months ago languageEnglish
more_vert

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:

ID,Text1,Text2
1,Record 1,Hello World!
2,Record 2,Hello Hadoop!
3,Record 3,"Hello 
Kontext!"
4,Record 4,Hello!

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

Environment 

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:
        print(row)

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)
print(pdf)

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.

copyright This page is subject to Site terms.

More from Kontext

local_offer teradata local_offer python

visibility 1035
thumb_up 1
access_time 4 months ago

Pandas is commonly used by Python users to perform data operations. In many scenarios, the results need to be saved to a storage like Teradata. This article shows you how to do that easily using JayDeBeApi or  ...

open_in_new Spark + PySpark

local_offer teradata local_offer python local_offer Java

visibility 584
thumb_up 0
access_time 4 months ago

Python JayDeBeApi module allows you to connect from Python to Teradata databases using Java JDBC drivers. In article Connect to Teradata database through Python , I showed ho...

open_in_new Python Programming

local_offer sqlite local_offer python local_offer Java

visibility 123
thumb_up 0
access_time 4 months ago

To read data from SQLite database in Python, you can use the built-in sqlite3 package . Another approach is to use SQLite JDBC driver via  ...

open_in_new Python Programming

local_offer python local_offer sqlite

visibility 72
thumb_up 0
access_time 4 months ago

SQLite is one of the most commonly used embedded file databases. All the mainstream programming language/framework provides APIs to interact with SQLite database. In my previous article  ...

open_in_new Python Programming

comment Comments (0)

comment Add comment

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

No comments yet.

Kontext Column

Created for everyone to publish data, programming and cloud related articles. Follow three steps to create your columns.


Learn more arrow_forward