[Numpy-discussion] Np.genfromtxt Problem

Andras Deak deak.andris at gmail.com
Fri Oct 4 18:15:37 EDT 2019


On Fri, Oct 4, 2019 at 7:31 PM Stephen P. Molnar <s.molnar at sbcglobal.net> wrote:
>
>
> I have a snippet of code
>
> #!/usr/bin/env python3
> # -*- coding: utf-8 -*-
> """
>
> Created on Tue Sep 24 07:51:11 2019
>
> """
> import numpy as np
>
> files = []
>
> data = np.genfromtxt(files, usecols=(3), dtype=None, skip_header=8,
> skip_footer=1, encoding=None)
>
> print(data)
>
>
> If file is a single file the code generates the data that I want.
> However I have a list of files that I want to process. According to
> numpy.genfromtxt fname can be a "File, filename, list, or generator to
> read."  If I use [13-7a_apo-1acl.RMSD    13-7_apo-1acl.RMSD
> 14-7_apo-1acl.RMSD    15-7_apo-1acl.RMSD    17-7_apo-1acl.RMSD ] get the
> error:

Hi Stephen,

As far as I know genfromtxt is designed to read the contents of a
single file. Consider this quote from the docs for the first
parameter:
"The strings in a list or produced by a generator are treated as lines."
And the general description of the function says
"Load data from a text file, with missing values handled as specified."
("a text file", singular)
So if I understand correctly the list case is there so that you can
pass `f.readlines()` or equivalent into genfromtxt. From a
higher-level standpoint, how would reading multiple files behave if
the files have different structure, and what type and shape should the
function return in that case?
If one file can be read just fine then I suggest looping over them to
read each, one after the other. You can then tell python what to do
with each returned array and so it doesn't have to guess.
Regards,

András




>
> runfile('/home/comp/Apps/Models/1-PhosphorusLigands/CombinedLigands/MOL/Docking/Results/RMSDTable/Test/DeltaGTable_s.py',
> wdir='/home/comp/Apps/Models/1-PhosphorusLigands/CombinedLigands/MOL/Docking/Results/RMSDTable/Test',
> current_namespace=True)
> Traceback (most recent call last):
>
>    File
> "/home/comp/Apps/Models/1-PhosphorusLigands/CombinedLigands/MOL/Docking/Results/RMSDTable/Test/DeltaGTable_s.py",
> line 12, in <module>
>      data = np.genfromtxt(files, usecols=(3), dtype=None, skip_header=8,
> skip_footer=1, encoding=None)
>
>    File
> "/home/comp/Apps/Miniconda3/lib/python3.7/site-packages/numpy/lib/npyio.py",
> line 1762, in genfromtxt
>      next(fhd)
>
> StopIteration
>
> I have tried very combination of search terms that I can think of in
> order to find an example of how to make this work without success.
>
> How can I make this work?
>
> Thanks in advance.
>
> --
> Stephen P. Molnar, Ph.D.
> www.molecular-modeling.net
> 614.312.7528 (c)
> Skype:  smolnar1
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion


More information about the NumPy-Discussion mailing list