2

我正在尝试使用 textwrap 来格式化导入文件,该文件的格式化方式非常特别。基本上如下(为简单起见缩短了行长):

abcdef <- Ok line
abcdef 
 ghijk <- Note leading space to indicate wrapped line
 lm

现在,我的代码如下:

wrapper = TextWrapper(width=80, subsequent_indent=' ', break_long_words=True, break_on_hyphens=False)
for l in lines:
  wrapline=wrapper.wrap(l)

这几乎完美地工作,但是,文本换行代码不会在 80 个字符标记处进行硬中断,它会尝试智能并在空格处中断(大约 20 个字符)。

我通过用唯一字符 (#) 替换字符串列表中的所有空格、包装它们然后删除字符来解决这个问题,但肯定有更清洁的方法吗?

注意任何可能的答案都需要在 Python 2.4 上工作 - 抱歉!

4

2 回答 2

1

听起来您正在禁用 TextWrapper 的大部分功能,然后尝试添加一些您自己的功能。我认为您最好编写自己的函数或类。如果我理解正确,您只是在寻找长度超过 80 个字符的行,并在 80 个字符处将它们断开,并将其余部分缩进一个空格。

例如,这个:

s = """\
This line is fine.
This line is very long and should wrap, It'll end up on a few lines.
A short line.
"""

def hard_wrap(s, n, indent):
    wrapped = ""
    n_next = n - len(indent)
    for l in s.split('\n'):
        first, rest = l[:n], l[n:]
        wrapped += first + "\n"
        while rest:
            next, rest = rest[:n_next], rest[n_next:]
            wrapped += indent + next + "\n"
    return wrapped

print hard_wrap(s, 20, " ")

产生:

This line is fine.
This line is very lo
 ng and should wrap,
  It'll end up on a
 few lines.
A short line.
于 2010-05-19T12:11:29.997 回答
1

基于生成器的版本对您来说可能是一个更好的解决方案,因为它不需要一次将整个字符串加载到内存中:

def hard_wrap(input, width, indent=' '):
   for line in input:
      indent_width = width - len(indent)
      yield line[:width]
      line = line[width:]
      while line:
         yield '\n' + indent + line[:indent_width]
         line = line[indent_width:]

像这样使用它:

from StringIO import StringIO # Makes strings look like files

s = """abcdefg
abcdefghijklmnopqrstuvwxyz"""

for line in hard_wrap(StringIO(s), 12):
   print line,

哪个打印:

abcdefg
abcdefghijkl 
 mnopqrstuvw 
 xyz
于 2010-07-11T00:11:32.420 回答