转到 Python 中的特定行? [英] Go to a specific line in Python?

查看:42
本文介绍了转到 Python 中的特定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转到 .txt 文件中的第 34 行并阅读它.你会如何在 Python 中做到这一点?

I want to go to line 34 in a .txt file and read it. How would you do that in Python?

推荐答案

使用 Python 标准库的 linecache 模块:

Use Python Standard Library's linecache module:

line = linecache.getline(thefilename, 33)

应该完全按照您的意愿行事.你甚至不需要打开文件——linecache 为你做这一切!

should do exactly what you want. You don't even need to open the file -- linecache does it all for you!

这篇关于转到 Python 中的特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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