String to html string python

When I print the string (in Python) coming from a website I scraped it from, it looks like this:

"His this 

is 

a sample

String"

It does not show the \n breaks. this is what I see in a Python interpreter.

And I want to convert it to HTML that will add in the line breaks. I was looking around and didn't see any libraries that do this out of the box.

I was thinking BeautifulSoup, but wasn't quite sure.

Nimantha

6,6266 gold badges24 silver badges62 bronze badges

asked Mar 9, 2019 at 16:07

Morgan AllenMorgan Allen

3,1157 gold badges52 silver badges83 bronze badges

If you have a String that you have readed it from a file you can just replace \n to <br>, which is a line break in html, by doing:

my_string.replace('\n', '<br>')

answered Mar 9, 2019 at 16:12

String to html string python

0

You can use the python replace(...) method to replace all line breaks with the html version <br> and possibly surround the string in a paragraph tag <p>...</p>. Let's say the name of the variable with the text is text:

html = "<p>" + text.replace("\n", "<br>") + "</p>"

answered Mar 9, 2019 at 16:30

String to html string python

Omari CelestineOmari Celestine

1,3431 gold badge10 silver badges20 bronze badges

searching for this answer in found this, witch is likely better because it encodes all characters, at least for python 3 Python – Convert HTML Characters To Strings

# import html
import html

# Create Text
text = 'Γeeks for Γeeks'

# It Converts given text To String
print(html.unescape(text))

# It Converts given text to HTML Entities
print(html.escape(text))

answered Sep 27 at 8:21

String to html string python

I believe this will work

for line in text:
   for char in line:
      if char == "/n":
         text.replace(char, "<br>")

answered Mar 9, 2019 at 16:16

String to html string python

tygzytygzy

5064 silver badges23 bronze badges

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Prerequisites: html module

    Given a string with HTML characters, the task is to convert HTML characters to a string. This can be achieved with the help of html.escape() method(for Python 3.4+), we can convert the ASCII string into HTML script by replacing ASCII characters with special characters by using html.escape()method.

    By this method we can decode the HTML entities into text.

    Syntax:

    html.unescape(String)

    We can also use Beautiful Soupwhich handles entity conversion. In Beautiful Soup 4, entities get decoded automatically.

    Example 1: Python 3.6+

    Python3

    import html

    text = 'Γeeks for Γeeks'

    print(html.unescape(text)) 

    print(html.escape(text)) 

    Output:

    Γeeks for Γeeks

    &Gamma;eeks for &Gamma;eeks

    Example 2: Python 2.6-3.3

    We can use HTMLParser.unescape() from the standard library:

    • For Python 2.6-2.7 it’s in HtmlParser.
    • For Python 3 it’s in html.parser

    Python3

    import html

    try:

        from HTMLParser import HTMLParser

    except ImportError:

        from html.parser import HTMLParser

    h = html.parser

    print(h.unescape('Γeeks for Γeeks'))  

    Output:

    Γeeks for Γeeks

    How do I convert a text file to HTML in Python?

    How to convert TXT to HTML.
    Install 'Aspose. Words for Python via . NET'..
    Add a library reference (import the library) to your Python project..
    Open the source TXT file in Python..
    Call the 'save()' method, passing an output filename with HTML extension..
    Get the result of TXT conversion as HTML..

    How do I get HTML data from Python?

    Sending an HTTP GET request to the URL of the webpage that you want to scrape, which will respond with HTML content. We can do this by using the Request library of Python. Fetching and parsing the data using Beautifulsoup and maintain the data in some data structure such as Dict or List.

    How do I convert a string to a string in Python?

    The str() function takes in any python data type and converts it into a string. But use of the str() is not the only way to do so. This type of conversion can also be done using the “%s” keyword, the .format function or using f-string function.

    How do you write HTML code in Python?

    How to write to an HTML file in Python?.
    Use the open file function to create the HTML file..
    Add input data in HTML format into the file with the help of the write function..
    Finally, save and close the file..