文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

-- 61.182.251.99 [DateTime(2004-09-23T16:58:36Z)] TableOfContents

描述

Reading Lines with Continuation Characters 读取含有行连接字符的文件行

问题 Problem

You have a file that includes long logical lines split over two or more physical lines, with backslashes to indicate that a continuation line follows. You want to process a sequence of logical lines, rejoining those split lines.

文件含有逻辑上很长的行,分隔在2个或者更多的物理文件行上,行尾使用'\'符号标记下一行仍然是逻辑上的同一行。需要处理逻辑行的序列,将分开的各物理行合并连接起来。

解决 Solution

As usual, a class is the right way to wrap this functionality in Python 2.1: Python 2.1中,正确方法仍是使用封装类:

class LogicalLines:

# Here's an example function, showing off usage: def show_logicals(fileob, numlines=5):

if _ _name_ _=='_ _main_ _':

r"""prima \ seconda \ terza quarta \ quinta sesta settima \ ottava """)

讨论 Discussion

This is another sequence-bunching problem, like Recipe 4.9. In Python 2.1, a class wrapper is the most natural approach to getting reusable code for sequence-bunching tasks. We need to support the sequence protocol ourselves and handle the sequence protocol in the sequence we wrap. In Python 2.1 and earlier, the sequence protocol is as follows: a sequence must be indexable by successively larger integers (0, 1, 2, ...), and it must raise an IndexError as soon as an integer that is too large is used as its index. So, if we need to work with Python 2.1 and earlier, we must behave this way ourselves and be prepared for just such behavior from the sequence we are wrapping.

In Python 2.2, thanks to iterators, the sequence protocol is much simpler. A call to the next method of an iterator yields its next item, and the iterator raises a StopIteration when it's done. Combined with a simple generator function that returns an iterator, this makes sequence bunching and similar tasks far easier:

from _ _future_ _ import generators

def logical_lines(fileobj):

参考 See Also