8000 Allow file comments with genfromtxt(..., names=True) by khaeru · Pull Request #351 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Allow file comments with genfromtxt(..., names=True) #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -1237,9 +1241,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.

Expand Down Expand Up @@ -1345,8 +1346,10 @@ 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:])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI -- .split takes a second optional argument. All of these should just become first_line.split(comments, 1), and the join parts can just go away.

first_values = split_line(first_line)
except StopIteration:
Expand Down
0