一种更加Pythonic的方式来在终端中显示标题

时间:2015-12-03 21:23:26

标签: python file

我已经编写了一个程序来读取数据文件并对它们做了一堆与这个问题无关的废话。在用户输入标题占用的行数后,它会在读取时自动跳过标题。

如果需要,我已经内置了一些功能来显示终端中的标题。以下是我曾经用过的功能性但又愚蠢的代码片段:

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?  ")

适当地给出:

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

是否有更高效的" Pythonic"怎么办呢?或者我的一切都会好起来的?

干杯!

2 个答案:

答案 0 :(得分:2)

不确定head和标头逻辑,但您可以使用itertools.islice拉出第一个header_length行,str.join加入输出:

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)]))

答案 1 :(得分:0)

我相信这是您正在寻找的功能:

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))