0

I am writing a Python program that reads a file and then writes its contents to another one, with added margins. The margins are user-input and the line length must be at most 80 characters.

I wrote a recursive function to handle this. For the most part, it is working. However, the 2 lines before any new paragraph display the indentation that was input for the right side, instead of keeping the left indentation. Any clues on why this happen?

Here's the code:

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))

For input of left_Margin=4 and right_Margin = 5, I expect this:

____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.

But The result is:

     ____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.
thebjorn
  • 26,297
  • 11
  • 96
  • 138
EAP
  • 31
  • 1
  • 8
  • Your question is not about asking the user for filenames so I've edited your question to remove code that didn't pertain. Your indentation and spacing were all over the place, so I fixed that too. Finally, `line` is a fine variable name for storing lines from a file, `i` is very surprising (since it's mostly used to store integers in a looping context). – thebjorn Sep 01 '19 at 18:12
  • It would be good if you provided the text of the input file too (not just how the output is supposed to look)... – thebjorn Sep 01 '19 at 18:14
  • Eva: The code currently in your question does not appear to produce the result you say you're currently getting. It's also unclear how and where the code detects and handles new paragraphs. Also, a @thebjorn has already mentioned, please provide the contents of the original input file. – martineau Sep 01 '19 at 18:41

1 Answers1

0

This isn't really a good fit for a recursive solution in Python. Below is an imperative/iterative solution of the formatting part of your question (I'm assuming you can take this and write it to a file instead). The code assumes that paragraphs are indicated by two consecutive newlines ('\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

the output of the above is:

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.
thebjorn
  • 26,297
  • 11
  • 96
  • 138
  • Thanks for your feedback! In my program, I am reading text from a file. How does this change the variable txt, since files don't take split()? Also, I bumped into a compiler issue, since it is no longer detecting the input file. Any ideas? – EAP Sep 01 '19 at 19:56
  • The structure of a text processing program is (1) read the text, (2) process the text, and (3) write the text. My code accomplishes (2). You can read the text into the `txt` variable, e.g. by `txt = open('inputfile.txt').read()`. For part (3) the print function has a `file=..` parameter that you can use. ps: I'm not going to provide a turnkey solution for your homework problem. If you have specific questions, you should ask specific questions (i.e. "...I bumped into a compiler issue..." is a _you_ problem, not a _me_ problem. Hope you understand ;-) – thebjorn Sep 01 '19 at 20:22