Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Encoding issues?

in my HTML file, the word "Schilde­rung" looks normally and it doesn't seem to have an (encoding?) problem. But when I copy the word, I get the following: "Schilde rung", and if I'd like to find out the length with python, I get 13 (instead of 12...).

What's the problem here, and how can I handle this?

Thanks a lot for any help!

EDIT: At the moment, I use the following: output.write(text.decode("utf-8")) This handles correctly all umlaut and other special char, but the above problem is still present. print(repr(txt)) gives: Schilde\xc2\xadrung How can we solve this problem? Thanks a lot!

like image 752
MarkF6 Avatar asked Aug 24 '26 22:08

MarkF6


1 Answers

There is U+00AD SOFT HYPHEN before r in the string:

>>> "Schilde­rung".decode('utf-8')
u'Schilde\xadrung'

To remove non-ascii characters:

>>> s = u'Schilde\xadrung'
>>> s.encode('ascii', 'ignore').decode()
u'Schilderung'
>>> len(_)
11
like image 189
jfs Avatar answered Aug 26 '26 12:08

jfs