From 97a38c4a4233fb133b2f2fa8b4fad9e65657f572 Mon Sep 17 00:00:00 2001 From: pierregm Date: Sun, 16 May 2010 23:05:30 +0000 Subject: * add a `replace_space` option to NameValidator * Force a file to be opened in 'U' mode (bug #1473) --- numpy/lib/_iotools.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'numpy/lib/_iotools.py') diff --git a/numpy/lib/_iotools.py b/numpy/lib/_iotools.py index b2a7d4729..c9d81048f 100644 --- a/numpy/lib/_iotools.py +++ b/numpy/lib/_iotools.py @@ -258,6 +258,8 @@ class NameValidator: * If 'lower', field names are converted to lower case. The default value is True. + replace_space: '_', optional + Character(s) used in replacement of white spaces. Notes ----- @@ -281,7 +283,8 @@ class NameValidator: defaultexcludelist = ['return', 'file', 'print'] defaultdeletechars = set("""~!@#$%^&*()-=+~\|]}[{';: /?.>,<""") # - def __init__(self, excludelist=None, deletechars=None, case_sensitive=None): + def __init__(self, excludelist=None, deletechars=None, + case_sensitive=None, replace_space='_'): # Process the exclusion list .. if excludelist is None: excludelist = [] @@ -303,6 +306,8 @@ class NameValidator: self.case_converter = lambda x: x.lower() else: self.case_converter = lambda x: x + # + self.replace_space = replace_space def validate(self, names, defaultfmt="f%i", nbfields=None): """ @@ -347,14 +352,16 @@ class NameValidator: deletechars = self.deletechars excludelist = self.excludelist case_converter = self.case_converter + replace_space = self.replace_space # Initializes some variables ... validatednames = [] seen = dict() nbempty = 0 # for item in names: - item = case_converter(item) - item = item.strip().replace(' ', '_') + item = case_converter(item).strip() + if replace_space: + item = item.replace(' ', replace_space) item = ''.join([c for c in item if c not in deletechars]) if item == '': item = defaultfmt % nbempty -- cgit v1.2.1