[Numpy-discussion] np.genfromtxt StopIteration Error

Bennet Fauber bennet at umich.edu
Fri Oct 11 13:12:05 EDT 2019


I think genfromtxt() wants a filename as the first argument, and you
have to tell it the entries in the file are strings not numerics.

test.py
------------------------------------------
import os
import glob
import numpy as np

fileList = []
filesList = []

for files in glob.glob("*.log"):
    fileName, fileExtension = os.path.splitext(files)
    fileList.append(fileName)
    filesList.append(files)

print('fileList = ', fileList)
print('filesList = ', filesList)

fname = '/tmp/foo.txt'
print('fname = ', fname)
data = np.genfromtxt(fname, dtype=str)
print(data)
------------------------------------------

Contents of /tmp/foo.txt
------------------------------------------
15-7.log
18-7.log
14-7.log
C-VX3.log
------------------------------------------

Sample run

$ python --version
Python 2.7.15+

$ python t.py
('fileList = ', ['15-7', '18-7', '14-7', 'C-VX3'])
('filesList = ', ['15-7.log', '18-7.log', '14-7.log', 'C-VX3.log'])
('fname = ', '/tmp/foo.txt')
['15-7.log' '18-7.log' '14-7.log' 'C-VX3.log']

Is that any help?


On Fri, Oct 11, 2019 at 12:41 PM Stephen P. Molnar
<s.molnar at sbcglobal.net> wrote:
>
> I have been fighting with the genfromtxt function in numpy for a while now and am trying a slightly different approach.
>
> Here is the code:
>
>
> import os
> import glob
> import numpy as np
>
> fileList = []
> filesList = []
>
> for files in glob.glob("*.log"):
> ?????? fileName, fileExtension = os.path.splitext(files)
> ?????? fileList.append(fileName)
> ?????? filesList.append(files)
>
> print('fileList = ', fileList)
> print('filesList = ', filesList)
>
> fname = filesList
> print('fname = ', fname)
> data = np.genfromtxt(fname, usecols=(1), skip_header=27, skip_footer=1, encoding=None)
> print(data)
>
> np.savetxt('fileList.dG', data, fmt='%12.9f', header='${d}')
> print(data.dG)
>
> I am using the Spyder IDE which has a variable explorer which shows:
>
> filesList = ['C-VX3.log', '18-7.log', '14-7.log', '15-7.log']
> fileList = ['C-VX3', '18-7', '14-7', '15-7']
>
> so the lists that genfromtxt needs are being generated.
>
> Goggling 'numpy genfromtxt stopiteration error' does not seem to address this problem. At least, I didn't find plaything that I thought applied.
>
> I would greatly appreciate some assistance here.
>
> Thanks is 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