× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Opening CSV files in Python

Gustavo Bragança
Aug 25, 2016
<p><img alt="Data" src="https://upload.wikimedia.org/wikipedia/commons/5/58/BinaryData50.png" style="float:left; height:200px; width:700px"></p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p>On this short tutorial, we will learn how to open CSV files using three different ways in Python. </p> <p>CSV stand for Comma-Separated Values and it looks like the one below:</p> <pre>0,5,7,3,7,1 2,8,6,5,2,1 7,3,6,9,8,7 0,3,7,5,8,4 3,8,5,3,7,6 1,4,8,3,9,7</pre> <p>In fact, we will be using this data for our examples. Note that all numbers are integers. The name of our files is <code>data.csv</code>.</p> <h1>Pure Python</h1> <p>This is a basic way of loading using only Python built-ins.</p> <pre>with open('data.csv') as f: read_file = f.read().split() data = [] for row in read_file: splitted_row = row.split(',') int_row = [] for val in splitted_row: int_row.append(int(val)) data.append(int_row) </pre> <p>In the first part, it reads the file and in the second part, it transforms the data into a list of list wich each value as an integer. Another way of getting the same result is with list comprehension:</p> <pre>with open('data.csv') as f: read_file = f.read().split() data = [[int(val) for val in row.split(',')]for row in read_file]</pre> <p>It is shorter but it can be a little trickier to understand at first.</p> <h1>CSV Package</h1> <p>Thankfully, Python already comes with a <a href="https://docs.python.org/3/library/csv.html">package</a> built to deal with CSV file.</p> <pre>import csv with open('data.csv') as f: file_read = csv.reader(f) data = [] for row in file_read: int_row = [] for val in row: int_row.append(int(val)) data.append(int_row) </pre> <p>Note that the <code>for</code> loops should be inside the <code>whit</code> statement or the file will be closed.</p> <p>We can also use list comprehension:</p> <pre>with open('data.csv') as f: file_read = csv.reader(f) data = [[int(val) for val in row]for row in file_read]</pre> <h1>Numpy</h1> <p><a href="http://www.numpy.org/">Numpy</a> is 3rd-Party package built for dealing with arrays. With it, our task is much easier.</p> <pre>import numpy as np data=np.loadtxt('data.csv', delimiter=',')</pre> <p> </p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus