-3

I have two strings in a Python script which each contain single lines of text, blank lines and multiple paragraphs. Some of the paragraphs in the strings are very long so I would like to split them into multiple lines of text so that each line in the paragraphs is a certain maximum width. I would then like to split each string into lines so that the strings may be compared using the HtmlDiff class in the difflib module. Might someone know a quick and easy way to do this? I would greatly appreciate it. Thanks so much.

José A.
  • 27
  • 5
  • python has module [textwrap](https://docs.python.org/3/library/textwrap.html). But you can always `split()` text into list or lines and use `for`-loop to check every line separatelly and add `'\n'` in too long lines, and later `join()` list of lines to create single text. – furas Jul 18 '22 at 14:03

1 Answers1

1

By searching, I found the following link: How to modify list entries during for loop?

Using the information in the first answer, and the first comment to this question, I was able to achieve what I was looking for using code as the following below:

firstListOfLines = firstText.splitlines()
  for index, line in enumerate(firstListOfLines):
    firstListOfLines[index] = textwrap.fill(line)
  firstListOfLines = '\n'.join(firstListOfLines).splitlines()

secondListOfLines = secondText.splitlines()
  for index, line in enumerate(secondListOfLines):
    secondListOfLines[index] = textwrap.fill(line)
  secondListOfLines = '\n'.join(secondListOfLines).splitlines()

Thanks so much. The first comment helped me to think about what to do. Thanks again.

José A.
  • 27
  • 5