我写了一个程序来读入数据文件,然后对它们做一些与这个问题无关的废话。当用户输入页眉占用的行数后读取时,它会自动跳过页眉。你知道吗
我已经内置了一些功能来显示终端中的标题,如果需要的话。下面是我用来做这件事的功能性但看起来很白痴的代码片段:
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”的方式去做这件事?还是我的就跟它能得到的一样好?你知道吗
干杯!你知道吗
我相信这就是你想要的功能:
不确定
head
和头逻辑,但可以使用itertools.islice
拉取第一行header_length
和str.join
来连接输出:相关问题 更多 >
编程相关推荐