Python 获取文件指定行内容

来源:互联网 发布:钢铁力量六级车数据 编辑:程序博客网 时间:2024/05/16 11:44

Python 中有一个读取文件的模块 linecache,用起来很方便。官网连接 https://docs.python.org/2/library/linecache.html

使用Python 读取指定行就是用的这个模块,该模块也有其它的方法,具体方法可以参考官网的说明,我这里只用了 linecache.getline(filename, lineno),读取文件指定行内容的代码如下

import linecache # get the  content of linetheline=linecache.getline("./test.csv",10) # 第一个参数指读取的文件,第二个参数指文件的行数print theline

本文出自“筱Mary”博客,转载请务必保留此处
http://blog.csdn.net/qq_31382921/article/details/77994410