Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1656f00

Browse files
committed
Merge pull request matplotlib#4455 from gatagat/fix_csv2rec_names
FIX: csv2rec for passing in both names and comments. fixed conflicts in mlab.py in favor of the incoming changes
1 parent 124472d commit 1656f00

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

lib/matplotlib/mlab.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,16 +2948,20 @@ def get_func(name, item, func):
29482948
'print' : 'print_',
29492949
}
29502950

2951-
def get_converters(reader):
2951+
def get_converters(reader, comments):
29522952

29532953
converters = None
2954-
for i, row in enumerate(reader):
2955-
if i==0:
2954+
i = 0
2955+
for row in reader:
2956+
if (len(row) and comments is not None and
2957+
row[0].startswith(comments)):
2958+
continue
2959+
if i == 0:
29562960
converters = [mybool]*len(row)
29572961
if checkrows and i>checkrows:
29582962
break
2959-
#print i, len(names), len(row)
2960-
#print 'converters', zip(converters, row)
2963+
i += 1
2964+
29612965
for j, (name, item) in enumerate(zip(names, row)):
29622966
func = converterd.get(j)
29632967
if func is None:
@@ -3009,7 +3013,7 @@ def get_converters(reader):
30093013
names = [n.strip() for n in names.split(',')]
30103014

30113015
# get the converter functions by inspecting checkrows
3012-
converters = get_converters(reader)
3016+
converters = get_converters(reader, comments)
30133017
if converters is None:
30143018
raise ValueError('Could not find any valid data in CSV file')
30153019

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)