我正在使用pyyaml将对象转储到文件中.对象中有几个unicode字符串.我以前做过这个,但现在它正在生成这样的输出项:
'item': !!python/unicode "some string"
Run Code Online (Sandbox Code Playgroud)
而不是期望的:
'item': 'some string'
Run Code Online (Sandbox Code Playgroud)
我打算输出为utf-8.我使用的当前命令是:
yaml.dump(data,file(suite_out,'w'),encoding='utf-8',indent=4,allow_unicode=True)
Run Code Online (Sandbox Code Playgroud)
在其他地方,我做以下工作:
codecs.open(suite_out,"w","utf-8").write(
yaml.dump(suite,indent=4,width=10000)
)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Python 2.7.3
edA*_*a-y 40
我尝试了很多组合,我能找到的唯一能够始终生成正确YAML输出的组合是:
yaml.safe_dump(data, file(filename,'w'), encoding='utf-8', allow_unicode=True)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10028 次 |
| 最近记录: |