numpy - Read text file only for certain rows to split up file in Python -
i loading text file using np.loadtxt , have python split in four. copy paste each set of data different text files , np.loadtxt each text file, i'm going have hundreds of times way time consuming.
here shortened version of text file. i'd have python read first number (0.6999) , discard it, read 5 following rows of values , assign variable names each column, , next 5 rows variables each column again, , on.
is there way tell python maybe np.loadtext row 1, row 2 6, 7 12 etc?
0.699999988 1 0.2000 0.0618 2 0.2500 0.0417 3 0.3000 0.0371 4 0.3500 0.0390 5 0.4500 0.0761 670.0000 169.4000 6.708e-09 635.0001 169.1806 1.584e-08 612.9515 168.6255 2.724e-08 591.2781 168.2719 4.647e-08 670.00 0.0e+00 0.0e+00 0.0e+00 0.0e+00 0.0e+00 0.0e+00 0.0e+00 635.00 9.8e-07 4.2e-07 2.1e-07 1.2e-07 4.4e-08 1.8e-08 1.4e-08 612.95 6.0e-06 3.5e-06 2.1e-06 1.3e-06 4.7e-07 1.8e-07 1.4e-07 591.28 2.2e-05 1.3e-05 7.7e-06 4.9e-06 1.8e-06 6.6e-07 5.0e-07 569.98 8.3e-05 5.0e-05 2.8e-05 1.8e-05 6.4e-06 2.4e-06 1.8e-06 549.06 3.0e-04 1.8e-04 1.0e-04 6.2e-05 2.3e-05 8.4e-06 6.4e-06 528.51 7.8e-04 5.0e-04 2.8e-04 1.7e-04 6.2e-05 2.3e-05 1.8e-05 508.34 1.6e-03 1.0e-03 5.8e-04 3.4e-04 1.3e-04 4.9e-05 3.7e-05
here using 3 different text files:
altvall,t,pp= np.loadtxt('file1.txt',usecols = (0,1,2),unpack=true) # load text file tau1,tau2,tau3,tau4,tau5,tau6,tau7 = np.loadtxt('file2.txt',usecols = (1,2,3,4,5,6,7),unpack=true) # load text file wvln,alb = np.loadtxt('file3.txt',usecols = (1,2),unpack=true) # load text file
now want similar without splitting text file different parts.
a simple way use itertools.izip_longest
group rows of input file groups of 5. key following:
for rows in izip_longest(*[file_object]*n): # rows tuple of n consecutive rows # rows
full example:
import numpy np itertools import izip_longest data = [] open(filehandle, 'r') fin: fin.next() # skip first line rows in izip_longest(*[fin]*5): # read fin 5 rows @ time rows = [map(float, r.strip().split()) r in rows] data.append(np.array(rows))
this yields list of 5xn arrays:
>>> print data [array([[ 1. , 0.2 , 0.0618], [ 2. , 0.25 , 0.0417], [ 3. , 0.3 , 0.0371], [ 4. , 0.35 , 0.039 ], [ 5. , 0.45 , 0.0761]]), array([[ 6.70000000e+02, 1.69400000e+02, 6.70800000e-09], [ 6.35000100e+02, 1.69180600e+02, 1.58400000e-08], [ 6.12951500e+02, 1.68625500e+02, 2.72400000e-08], [ 5.91278100e+02, 1.68271900e+02, 4.64700000e-08], [ 5.69980100e+02, 1.68055300e+02, 7.85900000e-08]]), array([[ 6.70000000e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00], [ 6.35000000e+02, 9.80000000e-07, 4.20000000e-07, 2.10000000e-07, 1.20000000e-07, 4.40000000e-08, 1.80000000e-08, 1.40000000e-08], [ 6.12950000e+02, 6.00000000e-06, 3.50000000e-06, 2.10000000e-06, 1.30000000e-06, 4.70000000e-07, 1.80000000e-07, 1.40000000e-07], [ 5.91280000e+02, 2.20000000e-05, 1.30000000e-05, 7.70000000e-06, 4.90000000e-06, 1.80000000e-06, 6.60000000e-07, 5.00000000e-07], [ 5.69980000e+02, 8.30000000e-05, 5.00000000e-05, 2.80000000e-05, 1.80000000e-05, 6.40000000e-06, 2.40000000e-06, 1.80000000e-06]])]
Comments
Post a Comment