Using multiple genfromtxt in one file

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:
  #Opening file, reading every fourth line starting with line 2.
  data = np.genfromtxt(itertools.islice(f_in,2,None,4),dtype=None,delimiter=",")
  #Storing lower transition designation:
  low = np.genfromtxt(itertools.islice(f_in,3,None,4),dtype=str)
  #Storing upper transition designation:
  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, , .

, , , . ?

+4
2

genfromtext (, , islice) . , : islice .

f_in.readlines(), hpaulj answer, f_in.seek(0) , reset . , , .

# Note: Untested code follows
with open(input_file) as f_in:
    np.genfromtxt(itertools.islice(f_in,2,None,4),dtype=None,delimiter=",")

    f_in.seek(0)  # Set the file pointer back to the beginning
    low = np.genfromtxt(itertools.islice(f_in,3,None,4),dtype=str)

    f_in.seek(0)  # Set the file pointer back to the beginning
    up = np.genfromtxt(itertools.islice(f_in,4,None,4),dtype=str)
+2

:

with open(input_file) as f_in:
  #Opening file, reading every fourth line starting with line 2.
  lines = f_in.readlines()
  data = np.genfromtxt(lines[2::4],dtype=None,delimiter=",")
  #Storing lower transition designation:
  low = np.genfromtxt(lines[3::4],dtype=str)
  #Storing upper transition designation:
  up = np.genfromtxt(lines[4::4],dtype=str)

islice , itertools , . . , islice tee repeat. , , .

tee:

with open('myfile.txt') as f:
    its = itertools.tee(f,2)
    print(list(itertools.islice(its[0],0,None,2)))
    print(list(itertools.islice(its[1],1,None,2)))

, .

+1

Source: https://habr.com/ru/post/1623820/


All Articles