Python numpy gen from txt question

Asked 2 years ago, Updated 2 years ago, 129 views

You are currently performing an operation to read the txt file to Python numpy. Other data are neatly gathered (second image), while only certain data (first image) are like this, so I'm having trouble reading the txt file. Isn't there a good way to read using numpy?

Below is the code I used to read the second image. dat = np.genfromtxt(f, encoding='ascii', names= (' ', 'MVAbase', 'PSSEver', ' ', ' ', 'freq', ' ', ' ' ), dtype=None, max_rows=1, comments='/')

    dat1 = np.genfromtxt(f, encoding='ascii', names= ('bus_num', 'bus_name', ' ', 'bus_type', ' ', ' ', ' ', 'bus_vol', 'bus_angle'), delimiter=",", dtype=None, max_rows= 24, usecols=(0,1,3,7,8), )

    dat2 = np.genfromtxt(f, encoding='ascii', names= ('load_bus_num', 'load_name', '', '', '', 'Pload', 'Qload'), delimiter=",", dtype=None, max_rows=16, skip_header = 1, usecols=(0,1,5,6))

    dat3 = np.genfromtxt(f, encoding='ascii', names= ('gen_bus_num', 'gen_name', 'Pgen', 'Qgen', 'Qmax','Qmin'), delimiter=",", dtype=float, max_rows=11, skip_header = 2, usecols=(0,1,2,3,4,5))

python numpy genfromtxt array

2022-09-21 16:10

1 Answers

It's troublesome, but wouldn't it be necessary to parse well?

Fortunately, I see a pattern of 4 lines, so after reading each line, I think I'll try to parse it by tying 4 lines of ', '.join(lines[i:i+4]) into one line, and then passing it to io.StringIO.


2022-09-21 16:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.