乔治·M·里 (Georges Méli) vs u'Georges M\xe9li\xe8s'
我读了十几页,但还是没明白。
这些版本之间的区别在哪里:
u'Georges Méliès'
和 u'Georges M\xe9li\xe8s'
以及如何将一个版本转换为另一个版本,反之亦然?
I've read a dozen pages but im still not getting it.
Where is the difference between these versions:
u'Georges Méliès'
and u'Georges M\xe9li\xe8s'
and how do convert one to the other and vice-versa?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些字符串被解释器解析后没有区别。
一种版本只是简单地添加特殊字符,但它要求源文件具有特殊的编码,例如UTF-8。
第二个版本将这些字符替换为其字节表示形式,因此在 ASCII 编码文件中包含此类字符串是安全的。
您不能谈论它们之间的“转换”,因为它们本质上是相同的字符串。但这可能是有趣的事情。
print u'Georges M\xe9li\xe8s'
给出输出Georges Méliès
和print repr(u'Georges Méliès')
给出u'Georges M\xe9li\xe8s'
There is no difference after those strings have been parsed by the interpreter.
One version simply puts the special characters, but it requires the source file to have a special encoding, such as UTF-8.
The second version replaces those characters with their byte representation, so it's safe to have such strings in an ASCII-encoded file.
You can't talk about "converting" between them, because they are essentially the same strings. But here is something that may be interesting.
print u'Georges M\xe9li\xe8s'
gives the outputGeorges Méliès
andprint repr(u'Georges Méliès')
givesu'Georges M\xe9li\xe8s'
这是相同的,我会添加:
u'Georges Méliès'.encode('latin1')
给出'Georges M\xe9li\xe8s'
It's the same, and I would add:
u'Georges Méliès'.encode('latin1')
gives'Georges M\xe9li\xe8s'