Programmer_nltk
Programmer_nltk

Reputation: 915

Preserving paragraph break while writing html file in python

new_phrase = open("d.txt").read()

with open("phrase.txt", "r") as f:
    words=f.read().splitlines()

for word in words:
    new_phrase = new_phrase.replace(word, f'<span style="color:Green;">{word}</span>')
    
with open("my.html","w") as fp:
   fp.write(new_phrase)

input("Press Enter to continue...")

How to retain paragraph break in html output. Now the original content containing three paragraphs is grouped into one big paragraph when I export it as html.

Output for print(new_phrase)

On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, <span style="color:Green;">pore</span> footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your <span style="color:Green;">current</span> document look.
You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the <span style="color:Green;">current</span> theme or using a format that you specify directly.
To change the overall look of your document, choose new Theme elements <span style="color:Green;">pour</span> on the Page Layout tab. To change the looks available in the Quick Style gallery, use the Change Current Quick Style Set command. Both the Themes gallery and the Quick Styles gallery provide reset commands so that you can always restore the look of your document to the original contained in your <span style="color:Green;">current</span> template.

HTMl output current

enter image description here

Upvotes: 0

Views: 43

Answers (1)

jsmills99
jsmills99

Reputation: 56

The splitlines() method strips newline characters by default. You can pass in the keyword argument keepends to keep the line endings:

>>> with open('test.txt') as f:
...     print(f.read().splitlines(keepends=True))
...
['line 1 \n', '\n', 'line 3\n']

You could also use the readlines() method on the file object itself:

>>> with open('test.txt') as f:
...     print(f.readlines())
...
['line 1 \n', '\n', 'line 3\n']

Upvotes: 2

Related Questions