From fe3ff1fe75e33ff03f0d07aea09419a2f910cb3a Mon Sep 17 00:00:00 2001 From: Alexandre Abraham Date: Mon, 1 Feb 2016 10:41:41 +0100 Subject: [PATCH] Robustify csv_to_array --- nilearn/_utils/numpy_conversions.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/nilearn/_utils/numpy_conversions.py b/nilearn/_utils/numpy_conversions.py index 434a9edd9d..a41c761f26 100644 --- a/nilearn/_utils/numpy_conversions.py +++ b/nilearn/_utils/numpy_conversions.py @@ -152,12 +152,14 @@ def csv_to_array(csv_path, delimiters=' \t,;', **kwargs): if not isinstance(csv_path, _basestring): raise TypeError('CSV must be a file path. Got a CSV of type: %s' % type(csv_path)) - # First, we try genfromtxt which works in most cases. - array = np.genfromtxt(csv_path, **kwargs) - if array.ndim <= 1 and np.all(np.isnan(array)): - # If the delimiter is not known genfromtxt generates an array full of - # nan. In that case, we try to guess the delimiter + try: + # First, we try genfromtxt which works in most cases. + array = np.genfromtxt(csv_path, loose=False, **kwargs) + except ValueError: + # There was an error during the conversion to numpy array, probably + # because the delimiter is wrong. + # In that case, we try to guess the delimiter. try: with open(csv_path, 'r') as csv_file: dialect = csv.Sniffer().sniff(csv_file.readline(), delimiters)