[英]How to skip first two lines and last one line in python
I have string value: 我有字符串值:
str1 = """
first line
second line
abc
def
xyz
123
lastline"""
How to skip first two lines and last line and print remaining lines. 如何跳过前两行和最后一行并打印剩余的行。
Code: I tried and works for to skip first two lines 代码:我尝试过并且为跳过前两行而工作
for index, line in enumerate(str1.split("\n")):
if index <= 1:
continue
print line
Output getting from above code: 从上面的代码得到的输出:
abc
def
xyz
123
lastline
Expected output: 预期产量:
abc
def
xyz
123
Use string.splitlines()
: 使用
string.splitlines()
:
>>> str1.splitlines()[3:-1]
[' abc', ' def', ' xyz', ' 123']
Note that the first line (index 0) is empty. 请注意,第一行(索引0)为空。 It is terminated by the first
\\n
directly after the opening double quotes. 它由双引号后的第一个
\\n
终止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.