1
A
回答
2
实验在Python 2.7.6解释:
a = u"string"
a
输出:u'string '
b = a.encode('utf-8').decode('utf-8')
b
输出:u'string'
b = a.decode('utf-8').encode('utf-8')
b
输出: '字符串'
a = "string"
a
输出: '串'
b = a.encode('utf-8').decode('utf-8')
b
输出:u'string”
b = a.decode('utf-8').encode('utf-8')
b
输出: '串'
而且,原字符串是否是Unicode与否,输出编码 - >解码将是一个Unicode字符串。 decode - > encode的输出不会是一个unicode字符串。尽管如此,值得注意的是,由于字符串是不可变的,所以除了检查UnicodeErrors之外,因为它没有捕获函数调用的返回值,所以您发布的代码行对于其他任何东西都是无用的。
编码 - >解码结构的唯一真正效果是所有通过它的字符串(并从返回中捕获)都是Unicode字符串。为什么你会想这样做,而不是unicode_string = unicode(normal_string, encoding='UTF-8')
我不知道。
相关问题
- 1. 编码UnicodeDecodeError的Python编码
- 2. Python的编码块
- 3. 的Python OBD编码
- 4. Python 2.5的编码
- 5. Python的Unicode编码
- 6. Python Unicode编码
- 7. python编码
- 8. Python JSON编码
- 9. python编码
- 10. 编码与Python
- 11. Python CSV编码
- 12. Python 2.7编码
- 13. 编码在Python
- 14. Python编码pipe.communicate
- 15. 编码:Java&Python
- 16. Python编码RPSLS
- 17. 的Python 3的urllib编码
- 18. python 2.7编码解码
- 19. python编码/解码问题
- 20. UTF8 python编码和解码
- 21. Python编码解码如何
- 22. Python素数编码
- 23. Python - 基本编码
- 24. python wx unicode编码
- 25. Python编码问题
- 26. Unicode编码在Python
- 27. python编码错误
- 28. python编码转换
- 29. Python&MySql:Unicode和编码
- 30. 基本python编码
你不是在这里正常化,所以我换了标签。不,绕过UTF-8不会有任何影响。 –
任何可以在编码中表示的字符串都会愉快地往返。 UTF-8 [可以代表任何Unicode代码点](http://en.wikipedia.org/wiki/UTF-8),这就是为什么它被设计的原因,所以这个代码应该是一个相当昂贵的无操作。 – holdenweb
它确实在Python 2中有所作为,对吧?在此之后,返回的值始终是Unicode,无论“somevar”是否为Python 2的“str”。 –