如何通过Python实例演示读取文件特定行内容的方法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计341个文字,预计阅读时间需要2分钟。
Python读取文件指定行内容,不使用数数,直接输出:
pythonimport linecache
text=linecache.getline('C:/Users/Administrator/Desktop/SourceCodeofMongoRedis/chapter_5/generate_string.py', 10)print(text.strip())
python读取文件指定行内容
import linecache text=linecache.getline(r'C:\Users\Administrator\Desktop\SourceCodeofMongoRedis\chapter_5\generate_string.py',10) 第十行内容为# info = '''1000001 王小小'''
实例扩展:
本文实例讲述了Python3实现从文件中读取指定行的方法。分享给大家供大家参考。具体实现方法如下:
''' ''' # Python的标准库linecache模块非常适合这个任务 import linecache the_line = linecache.getline('d:/FreakOut.cpp', 222) print (the_line) # linecache读取并缓存文件中所有的文本, # 若文件很大,而只读一行,则效率低下。
本文共计341个文字,预计阅读时间需要2分钟。
Python读取文件指定行内容,不使用数数,直接输出:
pythonimport linecache
text=linecache.getline('C:/Users/Administrator/Desktop/SourceCodeofMongoRedis/chapter_5/generate_string.py', 10)print(text.strip())
python读取文件指定行内容
import linecache text=linecache.getline(r'C:\Users\Administrator\Desktop\SourceCodeofMongoRedis\chapter_5\generate_string.py',10) 第十行内容为# info = '''1000001 王小小'''
实例扩展:
本文实例讲述了Python3实现从文件中读取指定行的方法。分享给大家供大家参考。具体实现方法如下:
''' ''' # Python的标准库linecache模块非常适合这个任务 import linecache the_line = linecache.getline('d:/FreakOut.cpp', 222) print (the_line) # linecache读取并缓存文件中所有的文本, # 若文件很大,而只读一行,则效率低下。

