encoding problems
tool69
kib2 at free.fr
Wed Aug 29 04:53:08 EDT 2007
Diez B. Roggisch a écrit :
> tool69 wrote:
>
>> Hi,
>>
>> I would like to transform reST contents to HTML, but got problems
>> with accented chars.
>>
>> Here's a rather simplified version using SVN Docutils 0.5:
>>
>> %-------------------------------------------------------------
>>
>> #!/usr/bin/env python
>> # -*- coding: utf-8 -*-
>
>
> This declaration only affects unicode-literals.
>
>> from docutils.core import publish_parts
>>
>> class Post(object):
>> def __init__(self, title='', content=''):
>> self.title = title
>> self.content = content
>>
>> def _get_html_content(self):
>> return publish_parts(self.content,
>> writer_name="html")["html_body"]
>> html_content = property(_get_html_content)
>
> Did you know that you can do this like this:
>
> @property
> def html_content(self):
> ...
>
> ?
>
I only took some part of code from someone else
(an old TurboGears tutorial if I remember).
But you're right : decorators are better.
>> # Instanciate 2 Post objects
>> p1 = Post()
>> p1.title = "First post without accented chars"
>> p1.content = """This is the first.
>> ...blabla
>> ... end of post..."""
>>
>> p2 = Post()
>> p2.title = "Second post with accented chars"
>> p2.content = """Ce poste possède des accents : é à ê è"""
>
>
> This needs to be a unicode-literal:
>
> p2.content = u"""Ce poste possède des accents : é à ê è"""
>
> Note the u in front.
>
>
> You need to encode a unicode-string into the encoding you want it.
> Otherwise, the default (ascii) is taken.
>
> So
>
> print post.html_content.encodec("utf-8")
>
> should work.
>
That solved it : thank you so much.
> Diez
More information about the Python-list
mailing list