[Tutor] Reading in large .dat file
Peter Otten
__peter__ at web.de
Wed Jun 29 13:41:13 EDT 2016
Colin Ross wrote:
> Good afternoon,
>
> I have a .dat file that is 214 rows by 65 columns that I would like to
> read into python and plot the 1st column versus all other columns.
>
> My code:
>
> #########################################################################
>
> import numpy as np
> import scipy
> import pylab as pl
> import matplotlib
> from matplotlib.ticker import ScalarFormatter, FormatStrFormatter
> import sys
>
> # Load in text from .dat file
>
> sed = np.loadtxt('spectra.dat', unpack = True)
for flux in sed:
pl.plot(wavelength, flux)
> pl.xscale('log')
>
> pl.show()
>
> #########################################################################
>
> This is fine if I want to write out a separate line for each of the 65
> columns, but I would like to simplify the code by looping over the data.
> Can someone please help me formatting the loop correctly?
To understand why the above works (assuming it does) consider that for many
data types
for item in data:
... # use item
is equivalent to
for index in range(len(data)):
item = data[index]
... # use item
Unrolling the above for len(data) == 3 gives:
item = data[0]
... # use first item
item = data[1]
... # use second item
item = data[2]
... # use third item
More information about the Tutor
mailing list