I am new to Python and am currently having problems processing my input files. Basically, I want my code to accept an input file, where the relevant information is contained in blocks of 4 lines. For my specific purpose, I only care about the information in lines 1-3 of each block.
The two-block input example I'm dealing with looks like this:
#Header line 1
#Header line 2
'Mn 1', 5130.0059, -2.765, 5.4052, 2.5, 7.8214, 1.5, 1.310, 2.390, 0.500, 8.530,-5.360,-7.630,
' LS 3d6.(5D).4p z6F*'
' LS 3d6.(5D).4d e6F'
'K07 A Kurucz MnI 2007 1 K07 1 K07 1 K07 1 K07 1 K07 1 K07 1 K07 1 K07 1 K07 Mn '
'Fe 2', 5130.0127, -5.368, 7.7059, 2.5, 10.1221, 2.5, 1.030, 0.860, 0.940, 8.510,-6.540,-7.900,
' LS 3d6.(3F2).4p y4F*'
' LS 3d5.4s2 2F2'
'RU Kurucz FeII 2013 4 K13 5 RU 4 K13 4 K13 4 K13 4 K13 4 K13 4 K13 4 K13 Fe+ '
I would prefer to store information from each of these three lines in separate arrays. Since the entries are a combination of strings and float, I use Numpy.genfromtxt to read the input file as follows:
import itertools
import numpy as np
with open(input_file) as f_in:
data = np.genfromtxt(itertools.islice(f_in,2,None,4),dtype=None,delimiter=",")
low = np.genfromtxt(itertools.islice(f_in,3,None,4),dtype=str)
up = np.genfromtxt(itertools.islice(f_in,4,None,4),dtype=str)
After executing the code, genfromtxt correctly reads information from the file for the first time. However, for the second and third calls to genfromtxt, I get the following warning
UserWarning: genfromtxt: Empty input file: "<itertools.islice object at 0x102d7a1b0>"
warnings.warn('genfromtxt: Empty input file: "%s"' % fname)
, , genfromtxt, , . genfromtxt, , .
, , , . ?