In [1]:
import csv
import pprint

In [2]:
with open('data/src/sample.csv') as f:
    print(f.read())


11,12,13,14
21,22,23,24
31,32,33,34

In [3]:
with open('data/src/sample.csv') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)


['11', '12', '13', '14']
['21', '22', '23', '24']
['31', '32', '33', '34']

In [4]:
with open('data/src/sample.csv') as f:
    reader = csv.reader(f)
    l = [row for row in reader]

In [5]:
print(l)


[['11', '12', '13', '14'], ['21', '22', '23', '24'], ['31', '32', '33', '34']]

In [6]:
print(l[1])


['21', '22', '23', '24']

In [7]:
print(l[1][1])


22

In [8]:
l_T = [list(x) for x in zip(*l)]
print(l_T)


[['11', '21', '31'], ['12', '22', '32'], ['13', '23', '33'], ['14', '24', '34']]

In [9]:
print(l_T[1])


['12', '22', '32']

In [10]:
print(l[0][0])


11

In [11]:
print(type(l[0][0]))


<class 'str'>

In [12]:
r = l[0]
print(r)


['11', '12', '13', '14']

In [13]:
print([int(v) for v in r])


[11, 12, 13, 14]

In [14]:
print([[int(v) for v in row] for row in l])


[[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34]]

In [15]:
with open('data/src/sample.csv') as f:
    reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
    l_f = [row for row in reader]

In [16]:
print(l_f)


[[11.0, 12.0, 13.0, 14.0], [21.0, 22.0, 23.0, 24.0], [31.0, 32.0, 33.0, 34.0]]

In [17]:
print(l_f[0][0])


11.0

In [18]:
print(type(l_f[0][0]))


<class 'float'>

In [19]:
with open('data/src/sample.txt') as f:
    print(f.read())


11 12 13 14
21 22 23 24
31 32 33 34

In [20]:
with open('data/src/sample.txt') as f:
    reader = csv.reader(f, delimiter=' ')
    l = [row for row in reader]

In [21]:
print(l)


[['11', '12', '13', '14'], ['21', '22', '23', '24'], ['31', '32', '33', '34']]

In [22]:
with open('data/src/sample_quote.csv') as f:
    print(f.read())


1,2,"3"
"a,b,c",x,y

In [23]:
with open('data/src/sample_quote.csv') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)


['1', '2', '3']
['a,b,c', 'x', 'y']

In [24]:
with open('data/src/sample_quote.csv') as f:
    reader = csv.reader(f, quoting=csv.QUOTE_NONE)
    for row in reader:
        print(row)


['1', '2', '"3"']
['"a', 'b', 'c"', 'x', 'y']

In [25]:
with open('data/src/sample_linebreak.csv') as f:
    print(f.read())


1,2,"3"
"a
b",x,y

In [26]:
with open('data/src/sample_linebreak.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)


['1', '2', '3']
['a\nb', 'x', 'y']

In [27]:
with open('data/src/sample_header_index.csv') as f:
    print(f.read())


,a,b,c,d
ONE,11,12,13,14
TWO,21,22,23,24
THREE,31,32,33,34

In [28]:
with open('data/src/sample_header_index.csv') as f:
    reader = csv.reader(f)
    l = [row for row in reader]

In [29]:
pprint.pprint(l)


[['', 'a', 'b', 'c', 'd'],
 ['ONE', '11', '12', '13', '14'],
 ['TWO', '21', '22', '23', '24'],
 ['THREE', '31', '32', '33', '34']]

In [30]:
print([row[1:] for row in l[1:]])


[['11', '12', '13', '14'], ['21', '22', '23', '24'], ['31', '32', '33', '34']]

In [31]:
print([[int(v) for v in row[1:]] for row in l[1:]])


[[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34]]