Skip to content

Commit f3bf9a8

Browse files
committed
Merge pull request #4455 from gatagat/fix_csv2rec_names
FIX: csv2rec for passing in both names and comments.
2 parents b8ea343 + 634de83 commit f3bf9a8

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/matplotlib/mlab.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2867,14 +2867,20 @@ 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)
28762880
if checkrows and i > checkrows:
28772881
break
2882+
i += 1
2883+
28782884
for j, (name, item) in enumerate(zip(names, row)):
28792885
func = converterd.get(j)
28802886
if func is None:
@@ -2925,7 +2931,7 @@ def get_converters(reader):
29252931
names = [n.strip() for n in names.split(',')]
29262932

29272933
# get the converter functions by inspecting checkrows
2928-
converters = get_converters(reader)
2934+
converters = get_converters(reader, comments)
29292935
if converters is None:
29302936
raise ValueError('Could not find any valid data in CSV file')
29312937

lib/matplotlib/tests/test_mlab.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,13 @@ 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.write('# comment\n1,2,3\n4,5,6\n')
344+
self.fd.seek(0)
345+
array = mlab.csv2rec(self.fd, names='a,b,c')
346+
assert len(array) == 2
347+
assert len(array.dtype) == 3
348+
342349

343350
class window_testcase(CleanupTestCase):
344351
def setUp(self):

0 commit comments

Comments
 (0)