I've written a program to read in data files and do a bunch of nonsense with them that is irrelevant to this question. It will automatically skip the header when reading after the user inputs how many lines the header occupies.
I have built in some functionality to display the header in the terminal, if requested. Here is the functional yet idiotic looking snippet of code I've used to do this:
filename = (raw_input("Which file are we loading? "))
with open(filename) as myfile:
head = 'head'
aline = myfile.readline()
bline = myfile.readline()
cline = myfile.readline()
dline = myfile.readline()
eline = myfile.readline()
fline = myfile.readline()
gline = myfile.readline()
hline = myfile.readline()
iline = myfile.readline()
jline = myfile.readline()
kline = myfile.readline()
lline = myfile.readline()
mline = myfile.readline()
nline = myfile.readline()
oline = myfile.readline()
pline = myfile.readline()
qline = myfile.readline()
rline = myfile.readline()
sline = myfile.readline()
tline = myfile.readline()
header = input("How many header lines? (Type ``head`` to see the first 20 lines) ")
if header == head:
print ' 1 | ' + aline,
print ' 2 | ' + bline,
print ' 3 | ' + cline,
print ' 4 | ' + dline,
print ' 5 | ' + eline,
print ' 6 | ' + fline,
print ' 7 | ' + gline,
print ' 8 | ' + hline,
print ' 9 | ' + iline,
print '10 | ' + jline,
print '11 | ' + kline,
print '12 | ' + lline,
print '13 | ' + mline,
print '14 | ' + nline,
print '15 | ' + oline,
print '16 | ' + pline,
print '17 | ' + qline,
print '18 | ' + rline,
print '19 | ' + sline,
print '20 | ' + tline,
header = input("How many header lines? ")
Which appropriately gives:
How many header lines? (Type ``head`` to see the first 20 lines) head
1 | ------------------------------------------------------------------------------------------------------------------------------------------------
2 | K-KIDS GOLD LIST
3 | ------------------------------------------------------------------------------------------------------------------------------------------------
4 |
5 | N = 1048 K dwarfs within 50 parsecs
6 |
...
...
...
20 | stuff
Is there a more efficient and "Pythonic" way to go about this? Or is mine as good as it's going to get?
Cheers!