0

I have a problem with special characters.

I am doing a mysql query using mysqldb. The database itself is in UTF-8 as well as the tables. The query has a result that, when running the query as a query itself, i get the correct result: Türkiye.

In MysqlDB, i am using the "charset = 'utf8', use_unicode = True" I also have "# -- coding: utf-8 --" At the top of my python file. Additionally, i also have the variable set to

unicode(text)

However, it seems that no matter what i do, i cannot get the text to display as "Türkiye" And it always is displaying as "T\xfcrkiye"

Now, i could do a simply find replace, but the problem is, there are many other special characters used in other places as well. Too many to reliably use find/replace. What i want to do, is have this character be displayed and stored correctly in a variable as "Türkiye" and not "T\xfcrkiye"

Thanks for the help!

11
  • Get it to display where? Commented Oct 21, 2014 at 22:14
  • On the web page itself/TEmplate Commented Oct 21, 2014 at 22:15
  • You're using {{ text }} and it's only displaying "T\xfcrkiye", without the quotes or any other characters? Commented Oct 21, 2014 at 22:17
  • Yes, that is what is happening. Commented Oct 21, 2014 at 22:22
  • To a bit more specific, it is displaying other characters as well ( when they are others) but it is displaying T\xfcrkiye instead of Türkiye If i type the text plainly in the template, it will display correctly. But getting it from a query, it does not. Commented Oct 21, 2014 at 22:29

2 Answers 2

1

Include this at the top of your HTML page/template

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

You should send the request as an html page, otherwise I don't think you will be able to output that character correctly.

Hope it works!

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

7 Comments

This didn't seem to work. As an aside, i noticed that if i change the line for the text to be: text.encode('utf-8') That it now displays as t\xc3\xbcrkiye on the webpage
you can try encoding it with 'latin-1' while displaying in your template >>> print u'\xce\xb1'.encode('latin-1') α see str.encode and Standard Encoding for supporting more languages and dialects
Doing that gives me an error 'latin-1' codec can't encode character u'\u0131' As i mentioned before, there are many other special characters in the text, i was just using the Türkiye as the example of the bunch
Well try other encoders some or the other will get a hit that's why I provided Standard Encoding list latin-1 usually takes care of many special alphabets try other encoders I'm sure some encoder will match.
i'm a bit confused by that though, utf-8 should take care of everything there. The problem is that if i do this and have just the single string or characters and use the utf-8 encoding, it appears to work fine, but when doing it from this query, for some reason it won't
|
0
In [5]: a = u"T\xfcrkiye"

In [6]: print a
Türkiye

I think you are printing str not unicode string. Try: var = u'{}'.format(text)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.