0

I am returning text field from DB column:

 "algún nombre de juego"

when I try to use this text in an html file, I get the following string:

algún nombre de juego

This happens consistently with any string that is not pure english. I have even tried grabbing simple text from online in different languages and as soon as I plug them into an html text box, the print out with improper encoding like the example above.

From reading Unicode documentation for python, UTF-8 should be able to handle pretty much any character in any language.

I have tried many ways of encoding/decoding, and either there is an encoding error, or I get back weird character where the letter u with acute should be.

Per comments, I am not using Django or Flask. Just taking strings from a DB that might be in several different languages and generating an HTML file for internal use.

4
  • HTML is not python. You need to convert the unicode characters to their HTML-entity equivalent (ú becomes ú) Commented Jan 21, 2021 at 20:53
  • @PranavHosangadi, am I correct to think that I need to pass a python string that is meant to be HTML through BeautifulSoup or something similar to make the conversion? Or is there a better approach? Commented Jan 21, 2021 at 20:58
  • 1
    @kravb It might be a good idea to also tag this question with the framework that you're (such as Django or Flask) using in order to get some more answers. Commented Jan 21, 2021 at 21:02
  • w3.org/International/questions/qa-html-encoding-declarations Commented Jan 21, 2021 at 21:05

1 Answer 1

1

A few ideas:

1. Remember to declare the encoding. Either:

  • Insert a <meta charset="utf-8"> just after your <head> tag.

    OR

  • do this in the HTTP header (making it look something like Content-Type:text/html; charset=UTF-8). This can be done by changing the webserver's settings or using server-side code, should you choose to host the page.

2. Ensure that your IDE saves files as UTF-8. This will be somewhere in your settings and should be done automatically, but worth checking if option 1 doesn't work.

Hope one of these works.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.