小编Han*_*uhn的帖子

PyYaml - 使用特殊字符(即重音符号)转储unicode

我正在使用yaml文件,这些文件必须是人类可读和可编辑的,但也可以从Python代码编辑.我正在使用Python 2.7.3

该文件需要处理重音(主要用于处理法语文本).

以下是我的问题示例:

import codecs
import yaml

file = r'toto.txt'

f = codecs.open(file,"w",encoding="utf-8")

text = u'héhéhé, hûhûhû'

textDict = {"data": text}

f.write( 'write unicode     : ' + text + '\n' )
f.write( 'write dict        : ' + unicode(textDict) + '\n' )
f.write( 'yaml dump unicode : ' + yaml.dump(text))
f.write( 'yaml dump dict    : ' + yaml.dump(textDict))
f.write( 'yaml safe unicode : ' + yaml.safe_dump(text))
f.write( 'yaml safe dict    : ' + yaml.safe_dump(textDict))

f.close()
Run Code Online (Sandbox Code Playgroud)

书面文件包含:

write unicode     : héhéhé, …
Run Code Online (Sandbox Code Playgroud)

python unicode yaml non-ascii-characters pyyaml

11
推荐指数
2
解决办法
5336
查看次数

标签 统计

non-ascii-characters ×1

python ×1

pyyaml ×1

unicode ×1

yaml ×1