Python:如何获取StringIO.writelines接受unicode字符串? [英] Python: How to get StringIO.writelines to accept unicode string?

查看:209
本文介绍了Python:如何获取StringIO.writelines接受unicode字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 34: ordinal not in range(128)

在下面存储在'a.desc'中的字符串上,因为它包含'£'字符.它以unicode字符串的形式存储在基础Google App Engine数据存储区中,这样就可以了. cStringIO.StringIO.writelines函数似乎正在尝试以ascii格式对其进行编码:

result.writelines(['blahblah',a.desc,'blahblahblah'])

如果这是正确的措辞,我如何指示它将编码视为unicode?

应用程序引擎在python 2.5上运行

解决方案

StringIO文档 :

与StringIO模块实现的存储文件不同,[cStringIO]提供的存储文件不能接受无法编码为纯ASCII字符串的Unicode字符串.

如果可能,请使用StringIO而不是cStringIO.

I'm getting a

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 34: ordinal not in range(128)

on a string stored in 'a.desc' below as it contains the '£' character. It's stored in the underlying Google App Engine datastore as a unicode string so that's fine. The cStringIO.StringIO.writelines function is trying seemingly trying to encode it in ascii format:

result.writelines(['blahblah',a.desc,'blahblahblah'])

How do I instruct it to treat the encoding as unicode if that's the correct phrasing?

app engine runs on python 2.5

解决方案

StringIO documentation:

Unlike the memory files implemented by the StringIO module, those provided by [cStringIO] are not able to accept Unicode strings that cannot be encoded as plain ASCII strings.

If possible, use StringIO instead of cStringIO.

这篇关于Python:如何获取StringIO.writelines接受unicode字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆