Search code examples
pythonpyyaml

pyyaml is producing undesired !!python/unicode output


I am using pyyaml to dump an object to a file. There are several unicode strings in the object. I've done this before, but now it's producing output items like this:

'item': !!python/unicode "some string"

Instead of the desired:

'item': 'some string'

I'm intending to output as utf-8. The current command I use is:

yaml.dump(data,file(suite_out,'w'),encoding='utf-8',indent=4,allow_unicode=True)

In other locations I do the following and it works:

codecs.open(suite_out,"w","utf-8").write(
    yaml.dump(suite,indent=4,width=10000)
)

What am I doing wrong?

Python 2.7.3


Solution

  • I tried many combinations and the only one I can find that consistently produces the correct YAML output is:

    yaml.safe_dump(data, file(filename,'w'), encoding='utf-8', allow_unicode=True)