Re: [Q] PyYAML: encoding option of yaml.dump()

Kirill Simonov <[email protected]>
Newsgroups gmane.text.yaml.general
Message-ID <[email protected]>
Makoto Kuwata wrote:
> I have question about encoding option of yaml.dump() of PyYAML.
> 
> I tried the following python script, and got '"\u30DD\u30B1\u30E2\u30F3"'
> as a result, thought what I expect is utf8 characters, not escaped sequence.
> 
>     import yaml
>     data = [u'\u30dd\u30b1\u30e2\u30f3']
>     print yaml.dump(data, default_flow_style=False)
>     print yaml.dump(data, default_flow_style=False, encoding='utf8')
>     ## (result)
>     ## - "\u30DD\u30B1\u30E2\u30F3"
>     ## - "\u30DD\u30B1\u30E2\u30F3"
>     ## (expected)
>     ## - "\u30DD\u30B1\u30E2\u30F3"
>     ## - XXXX  # japanese characters in utf8
> 
> Is it possible to get utf8 characters using yaml.dump() ?

By default, PyYAML does not emit non-ASCII characters unescaped.  You 
need to set the flag allow_unicode to print Japanese characters:

print yaml.dump(data, default_flow_style=False, allow_unicode=True)


Thanks,
Kirill

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.