Python 读取文本文件,怎么才能读取一段内容
发布网友
发布时间:2022-05-03 10:44
我来回答
共1个回答
热心网友
时间:2022-06-19 03:16
python读取段落需要自定义函数:
from _ _future_ _ import generators
def paragraphs(fileobj, separator='\n'):
if separator[-1:] != '\n': separator += '\n' paragraph = []
for line in fileobj:
if line == separator:
if paragraph: yield ''.join(paragraph)
paragraph = []
else: paragraph.append(line)
if paragraph: yield ''.join(paragraph)