在python中获取第n行字符串 [英] get nth line of string in python

查看:40
本文介绍了在python中获取第n行字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Python 3 中获取字符串的 nth 行?例如

How can you get the nth line of a string in Python 3? For example

getline("line1\nline2\nline3",3)

有没有办法使用 stdlib/内置函数来做到这一点?我更喜欢 Python 3 中的解决方案,但 Python 2 也不错.

Is there any way to do this using stdlib/builtin functions? I prefer a solution in Python 3, but Python 2 is also fine.

推荐答案

尝试以下操作:

s = "line1\nline2\nline3"
print s.splitlines()[2]

这篇关于在python中获取第n行字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆