[英]Why do I get the u“xyz” format when I print a list of unicode strings in Python?
[英]Why is it when I print something, there is always a unicode next to it? (Python)
[u'Iphones', u'dont', u'receieve', u'messages']
有没有办法打印它没有前面的“你”?
您所看到的是unicode字符串的__repr__()
表示,其中包含用于清除它的u。 如果你不想要你,你可以打印对象(使用__str__
) - 这对我__str__
:
print [str(x) for x in l]
可能更好的是阅读python unicode并使用您想要的特定unicode编解码器进行编码:
print [x.encode() for x in l]
[编辑]:澄清repr以及为什么你在那里 - repr的目标是提供一个方便的字符串表示,“返回一个字符串,当传递给eval()时会产生一个具有相同值的对象”。 即,您可以复制并粘贴打印输出并获取相同的对象(unicode字符串列表)。
Python包含unicode字符串和常规字符串的字符串类。 字符串前面的u表示它是一个unicode字符串。
>>> mystrings = [u'Iphones', u'dont', u'receieve', u'messages']
>>> [str(s) for s in mystrings]
['Iphones', 'dont', 'receieve', 'messages']
>>> type(u'Iphones')
<type 'unicode'>
>>> type('Iphones')
<type 'str'>
有关Python中可用字符串类型的更多信息,请参阅http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.