Skip to content

Commit 90942c9

Browse files
committed
Fix csv2rec for passing in both names and comments.
1 parent b8ea343 commit 90942c9

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/matplotlib/mlab.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2867,12 +2867,17 @@ def get_func(name, item, func):
28672867
'print': 'print_',
28682868
}
28692869

2870-
def get_converters(reader):
2870+
def get_converters(reader, comments):
28712871

28722872
converters = None
2873-
for i, row in enumerate(reader):
2873+
i = 0
2874+
for row in reader:
2875+
if (len(row) and comments is not None and
2876+
row[0].startswith(comments)):
2877+
continue
28742878
if i == 0:
28752879
converters = [mybool]*len(row)
2880+
i += 1
28762881
if checkrows and i > checkrows:
28772882
break
28782883
for j, (name, item) in enumerate(zip(names, row)):
@@ -2925,7 +2930,7 @@ def get_converters(reader):
29252930
names = [n.strip() for n in names.split(',')]
29262931

29272932
# get the converter functions by inspecting checkrows
2928-
converters = get_converters(reader)
2933+
converters = get_converters(reader, comments)
29292934
if converters is None:
29302935
raise ValueError('Could not find any valid data in CSV file')
29312936

lib/matplotlib/tests/test_mlab.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,14 @@ def test_rec2csv_bad_shape_ValueError(self):
339339
# the bad recarray should trigger a ValueError for having ndim > 1.
340340
assert_raises(ValueError, mlab.rec2csv, bad, self.fd)
341341

342+
def test_csv2rec_names_with_comments(self):
343+
self.fd.seek(0)
344+
self.fd.write('# comment\n1,2,3\n4,5,6\n')
345+
self.fd.seek(0)
346+
array = mlab.csv2rec(self.fd, names='a,b,c')
347+
assert len(array) == 2
348+
assert len(array.dtype) == 3
349+
342350

343351
class window_testcase(CleanupTestCase):
344352
def setUp(self):

0 commit comments

Comments
 (0)