第六章:文件系统-linecache:高效读取文本文件-处理空行

6.5.3 处理空行
返回值总算在行末尾包含一个换行符,所以如果文本行为空,则返回值就是一个换行符。

import linecache
from linecache_data import *

filename = make_tempfile()

# Blank lines include the newline.
print('BLANK :{!r}'.format(linecache.getline(filename,8)))

cleanup(filename)

输入文件的第八行不包含任何文本。
运行结果:

BLANK :’\n’

猜你喜欢

转载自blog.csdn.net/weixin_43193719/article/details/88539460