When to use numpy, csv and pandas, reading a file (2D array) in Python?
<p>There are several ways to read a file whose data is a 2D array. </p>

<ul>
<li>read as a list of lists/tuples</li>
<li>use the module <strong>csv</strong></li>
<li>use the module <strong>numpy</strong></li>
<li>use the module <strong>pandas</strong></li>
</ul>

<p>What are their application scenarios?</p>

<hr>

<pre><code>#!/usr/bin/env python
# -*- coding: utf-8 -*-

filename = 'test.txt'
# the content of `test.txt`
'''
a   b   c
d   e   f
g   h   i
j   k   l
'''

# Way 1: read as a list of lists/tuples
lists = list()
with open(filename) as fp:
    for line in fp:
        lists.append(line.split())

# Way 2: use the module csv
import csv

with open(filename) as fp:
    reader = csv.reader(fp, delimiter="\t")
    #table = [line for line in reader] # [['a   b   c'], ['d   e   f'], ['g   h   i'], ['j   k   l']]
    table = [line[0].split() for line in reader]

# Way 3: use the module numpy
import numpy as np

table = np.loadtxt(filename, dtype='str')

# Way 4: use the module pandas
import pandas as pd
table = pd.read_csv(filename)
</code></pre>