From 9f45975d5b7f95383da42e83bdfc9f6bd243a697 Mon Sep 17 00:00:00 2001 From: Paul Natsuo Kishimoto Date: Thu, 12 Jul 2012 16:42:59 -0400 Subject: [PATCH 1/2] Allow file comments with genfromtxt(..., names=True) --- numpy/lib/npyio.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index b63003f80795..1081285ce17d 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1237,9 +1237,6 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, ----- * When spaces are used as delimiters, or when no delimiter has been given as input, there should not be any missing data between two fields. - * When the variables are named (either by a flexible dtype or with `names`, - there must not be any header in the file (else a ValueError - exception is raised). * Individual values are not stripped of spaces by default. When using a custom converter, make sure the function does remove spaces. @@ -1347,7 +1344,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, first_line = fhd.next() if names is True: if comments in first_line: - first_line = asbytes('').join(first_line.split(comments)[1:]) + first_line = first_line.split(comments)[0] first_values = split_line(first_line) except StopIteration: # return an empty array if the datafile is empty From 74e071e81acb81b4030ed0ea86918715adb1e090 Mon Sep 17 00:00:00 2001 From: Paul Natsuo Kishimoto Date: Mon, 16 Jul 2012 14:42:31 -0400 Subject: [PATCH 2/2] Un-break test, move behaviour to skip_header kwarg --- numpy/lib/npyio.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 1081285ce17d..4e9089958547 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1189,11 +1189,15 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, Which columns to read, with 0 being the first. For example, ``usecols = (1, 4, 5)`` will extract the 2nd, 5th and 6th columns. names : {None, True, str, sequence}, optional - If `names` is True, the field names are read from the first valid line - after the first `skip_header` lines. - If `names` is a sequence or a single-string of comma-separated names, - the names will be used to define the field names in a structured dtype. - If `names` is None, the names of the dtype fields will be used, if any. + Field names for structured dtype output. May be one of: + + - True: field names are read from the first line after the initial + `skip_header` lines. If that line is commented and `skip_header` is + not -1, the portion *after* `comments` is used. + - None: field names from the `dtype` argument are used, if any. + - A sequence: field names are taken from the sequence. + - A string: comma-separated substrings are used as field names. + excludelist : sequence, optional A list of names to exclude. This list is appended to the default list ['return','file','print']. Excluded names are appended an underscore: @@ -1342,9 +1346,11 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, try: while not first_values: first_line = fhd.next() - if names is True: - if comments in first_line: + if names is True and comments in first_line: + if skip_header == -1: first_line = first_line.split(comments)[0] + else: + first_line = asbytes('').join(first_line.split(comments)[1:]) first_values = split_line(first_line) except StopIteration: # return an empty array if the datafile is empty