1

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!

2 Answers 2

2

Not sure on the head and header logic but you can use itertools.islice to pull the first header_length lines and str.join to join the output:

from itertools import islice

filename = raw_input("Which file are we loading?  "))
# ask user how many header lines
header_length = int(raw_input("Enter amount of header lines"))

with open(filename) as myfile:
    # get the first  header_length lines in a list
    head = list(islice(myfile, header_length))
    header = raw_input("How many header lines? (Type ``head`` to see the header lines)")
     # if user types head
    if "head" == header:
        # use enumerate to get the line numbers/index in list 
        # the str.join the lines formatting index | line
        print("".join(["{} | {}".format(i,  line) for i, line in enumerate(head,start=1)]))
Sign up to request clarification or add additional context in comments.

8 Comments

Slight nitpick, but you're missing the |
@KronoS, edited it out, it actually needs to be in format
you're second raw input is redundant with the first one... I think.
@KronoS. The first gets the filename, the second the amount of header lines and the last asks whether to display those lines, we are skipping the header regardless, the only difference is if we dispaly it or not
Thanks! I'm going to assume this works and give you an "up," but I'm really new to Python and I don't understand the last line at all.
|
0

I believe that this is the functionality you're looking for:

filename = (raw_input("Which file are we loading?  "))
with open(filename) as myfile:

    file_lines = myfile.readlines()  # save all lines from file into memory

header = raw_input("How many header lines? (Type ``head`` to see the first 20 lines)  ")

num_to_print = 20 if header == 'head' else int(header)  # get number of lines to be read.  if 'head' then 20

for i, line in enumerate(file_lines[:num_to_print]):
    print("{:02}|{}".format(i, line))

2 Comments

Why cast header to int twice and how can you cast "head" to an int?
You could also just use myfile.readlines() if you are going to store all the lines

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.