python字符串unicode问题

标签 python string unicode

这是我的代码(我使用的是 python 2.7)

    result = " '{0}' is unicode or something: ".format(mongdb['field'])
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 27: ordinal not in range(128)

看起来我从 mongodb 读取的字符串包含 unicode。它抛出这个错误。如何修复它以将此 unicde 与自定义字符串“is unicode or something:”连接起来?

提前致谢

更新

result = u" '{0}' is unicode or something: ".format(mongdb['field']) 

对我有用

最佳答案

使用unicode格式字符串(推荐):

result = u" '{0}' is unicode or something: ".format(mongdb['field'])

或者对字段进行编码:

result = " '{0}' is unicode or something: ".format(mongdb['field'].encode('utf-8'))

关于python字符串unicode问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18796174/

相关文章:

c++ - 从 C++ 文件中读取 Unicode 字符

python - 将圆切成相等的部分,Python

python - 摘要用户django迁移错误

python - BeautifulSoup | python | URL循环

php - MySQL 中的希腊字符搜索功能

c++ - Unicode - 通常在 C++ 中使用它

python os.path.isdir() 在 Windows 上运行缓慢

c++ - 为什么字符串变量打印有垃圾

javascript - 从 JavaScript 中的文本返回数组

c - scanf 没有正确存储输入