0

我正在编写一个 Python 程序,它读取一个文件,然后将其内容写入另一个文件,并增加了边距。边距是用户输入的,行长不得超过 80 个字符。

我写了一个递归函数来处理这个问题。在大多数情况下,它正在工作。但是,任何新段落之前的 2 行显示为右侧输入的缩进,而不是保留左侧缩进。关于为什么会发生这种情况的任何线索?

这是代码:

left_Margin = 4
right_Margin = 5

# create variable to hold the number of characters to withhold from line_Size
avoid = right_Margin
num_chars = left_Margin


def insertNewlines(i, line_Size):
    string_length = len(i) + avoid + right_Margin
    if len(i) <= 80 + avoid + left_Margin:
        return i.rjust(string_length)
    else:
        i = i.rjust(len(i)+left_Margin)
        return i[:line_Size] + '\n' + ' ' * left_Margin + insertNewlines(i[line_Size:], line_Size)

with open("inputfile.txt", "r") as inputfile:
    with open("outputfile.txt", "w") as outputfile:
        for line in inputfile:
            num_chars += len(line)
            string_length = len(line) + left_Margin
            line = line.rjust(string_length)
            words = line.split()
            # check if num of characters is enough
            outputfile.write(insertNewlines(line, 80 - avoid - left_Margin))

对于 left_Margin=4 和 right_Margin = 5 的输入,我希望这样:

____Poetry is a form of literature that uses aesthetic and rhythmic
____qualities of language—such as phonaesthetics, sound symbolism, and
____metre—to evoke meanings in addition to, or in place of, the prosai
____c ostensible meaning. 
____Poetry has a very long history, dating back to prehistorical ti
____mes with the creation of hunting poetry in Africa, and panegyric an
____d elegiac court poetry was developed extensively throughout the his
____tory of the empires of the Nile, Niger and Volta river valleys.

但结果是:

     ____Poetry is a form of literature that uses aesthetic and rhythmic
     ______qualities of language—such as phonaesthetics, sound symbolism, and
     ______metre—to evoke meanings in addition to, or in place of, the prosai
          ________c ostensible meaning. 
    _____Poetry has a very long history, dating back to prehistorical ti
    _____mes with the creation of hunting poetry in Africa, and panegyric an
    _____d elegiac court poetry was developed extensively throughout the his
          _____tory of the empires of the Nile, Niger and Volta river valleys.
4

1 回答 1

0

这并不适合 Python 中的递归解决方案。以下是您问题的格式化部分的命令式/迭代解决方案(我假设您可以将其写入文件)。该代码假定段落由两个连续的换行符 ( '\n\n') 指示。

txt = """
Poetry is a form of literature that uses aesthetic and rhythmic qualities of language—such as phonaesthetics, sound symbolism, and metre—to evoke meanings in addition to, or in place of, the prosaic ostensible meaning.

Poetry has a very long history, dating back to prehistorical times with the creation of hunting poetry in Africa, and panegyric and elegiac court poetry was developed extensively throughout the history of the empires of the Nile, Niger and Volta river valleys.
"""


def format_paragraph(paragraph, length, left, right):
    """Format paragraph ``p`` so the line length is at most ``length``
       with ``left`` as the number of characters for the left margin,
       and similiarly for ``right``.
    """
    words = paragraph.split()
    lines = []
    curline = ' ' * (left - 1)  # we add a space before the first word

    while words:
        word = words.pop(0)  # process the next word

        # +1 in the next line is for the space.
        if len(curline) + 1 + len(word) > length - right:
            # line would have been too long, start a new line
            lines.append(curline)
            curline = ' ' * (left - 1)
        curline += " " + word

    lines.append(curline)

    return '\n'.join(lines)


# we need to work on one paragraph at a time
paragraphs = txt.split('\n\n')

print('0123456789' * 8)  # print a ruler..

for paragraph in paragraphs:
    print(format_paragraph(paragraph, 80, left=4, right=5))
    print()  # next paragraph

上面的输出是:

01234567890123456789012345678901234567890123456789012345678901234567890123456789
    Poetry is a form of literature that uses aesthetic and rhythmic
    qualities of language such as phonaesthetics, sound symbolism, and
    metre to evoke meanings in addition to, or in place of, the prosaic
    ostensible meaning.

    Poetry has a very long history, dating back to prehistorical times with
    the creation of hunting poetry in Africa, and panegyric and elegiac
    court poetry was developed extensively throughout the history of the
    empires of the Nile, Niger and Volta river valleys.
于 2019-09-01T18:47:31.197 回答