[lang-ref] ( parse_csv_to_array ) ( python )
def test_parse_csv_to_array():
# csv.reader
import csv
text = '''\
A,B,C,D
11,12,13,14
21,22,23,24
31,32,33,34
'''
expected = [
['A', 'B', 'C', 'D'],
['11', '12', '13', '14'],
['21', '22', '23', '24'],
['31', '32', '33', '34'],
]
# with open('a.csv') as f:
with io.StringIO(text) as f:
reader = csv.reader(f)
rows = []
for row in reader:
rows.append(row)
assert rows == expected
# In this case, reading all rows at once is sufficient
# with open('a.csv') as f:
with io.StringIO(text) as f:
rows = list(csv.reader(f))
assert rows == expected