[英]List certain lines from a text file in python
The following is my list function: 以下是我的列表函数:
def list():
f = open("List.txt", 'r')
lines = f.readlines()
print("\n" + "Reading List...")
print(lines)
for line in lines:
print(line)
f.close()
intro()
I want to print from the range 5 (so the 6th line), to the length of the lines. 我想从范围5(即第6行)到行的长度进行打印。 I have tried: 我努力了:
for line in range(5,len(lines)):
But that didn't work and only outputted the number of where the line is, not the line itself. 但这是行不通的,只输出行所在的位置的编号,而不是行本身的编号。 I am fairly new to python and am unsure how I would go around doing this. 我对python相当陌生,不确定如何进行此操作。 Please give constructive criticism where possible. 请尽可能提出建设性的批评。 I am using a text file. 我正在使用一个文本文件。
You can achieve that by doing something like 您可以通过执行以下操作来实现
for line in lines[5:]:
print(line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.