If I have data in a text file that looks like:
# this is a header
# and so is this
#
alligator 27.2 83.4
bear 23.9 90.2
cat 12.56 0.98
dog 15.97 0.88884
...
...
...I know I can read that data in (make a list of lists corresponding to the lines of data) by using the following block of code:
file1 = 'tmp.txt'
file1_data = []
data_input = open(file1,'r')
for line in data_input:
if "#" not in line:
line = line.strip().split()
first_col_datum = line[0]
second_col_datum = float(line[1])
third_col_datum = float(line[2])
file1_data.append([first_col_datum,second_col_datum,third_col_datum])
data_input.close()
...but my intuition tells me there is a much much more elegant way to complete this task. Basically I would like to read in the file line by line, ignore '#'s, and supply the command with a 'format' for each element in the line (like ["%s","%0.6f","%0.6f","%0.6f","%i"] or something...I will always know this a priori). What is the best practice to do this?