在termin中显示标题的一种更像python的方式

2024-09-22 16:35:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个程序来读入数据文件,然后对它们做一些与这个问题无关的废话。当用户输入页眉占用的行数后读取时,它会自动跳过页眉。你知道吗

我已经内置了一些功能来显示终端中的标题,如果需要的话。下面是我用来做这件事的功能性但看起来很白痴的代码片段:

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

有没有一个更有效和“Python”的方式去做这件事?还是我的就跟它能得到的一样好?你知道吗

干杯!你知道吗


Tags: inputreadlinefilenamemyfileheadmanyhowheader
2条回答

我相信这就是你想要的功能:

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

不确定head和头逻辑,但可以使用itertools.islice拉取第一行header_lengthstr.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)]))

相关问题 更多 >