[英]How can I slice a substring from a unicode string with Python?
I have a unicode string as a result : u'splunk>\\xae\'
结果我有一个unicode字符串: u'splunk>\\xae\'
How can I get the substring 'uf001' 如何获得子字符串“ uf001”
as a simple string in python? 作为python中的简单字符串?
The characters uf001
are not actually present in the string, so you can't just slice them off. 字符uf001
实际上不存在于字符串中,因此您不能仅将它们切掉。 You can do 你可以做
repr(s)[-6:-1]
or 要么
'u' + hex(ord(s[-1]))[2:]
Since you want the actual string (as seen from comments) , just get the last character [-1] index
, Example - 由于您需要实际的字符串(从注释中看到),因此只需获取最后一个字符[-1] index
,示例-
>>> a = u'splunk>\xae\uf001'
>>> print(a)
splunk>®ï€
>>> a[-1]
'\uf001'
>>> print(a[-1])
ï€
If you want the unicode representation ( \
) , then take repr(a[-1])
, Example - 如果要使用unicode表示形式( \
),请使用repr(a[-1])
,示例-
>>> repr(a[-1])
"'\\uf001'"
\
is a single unicode character (not multiple strings) , so you can directly get that character as above. \
是单个unicode字符(而不是多个字符串),因此您可以如上所述直接获取该字符。
You see \
because you are checking the results of repr()
on the string, if you print it, or use it somewhere else (like for files, etc) it will be the correct \
character. 之所以会看到\
是因为您正在检查字符串上repr()
的结果,如果打印它,或者在其他地方使用它(如文件等),它将是正确的\
字符。
u''
it is how a Unicode string is represented in Python source code. u''
这就是在Python源代码中表示Unicode字符串的方式。 REPL uses this representation by default to display unicode objects: 默认情况下,REPL使用此表示形式来显示unicode对象:
>>> u'splunk>\xae\uf001'
u'splunk>\xae\uf001'
>>> print(u'splunk>\xae\uf001')
splunk>®
>>> print(u'splunk>\xae\uf001'[-1])
If your terminal is not configured to display Unicode or if you are on a narrow build (eg, it is likely for Python 2 on Windows) then the result may be different. 如果您的终端未配置为显示Unicode,或者您的构建版本较窄(例如Windows上的Python 2很有可能),则结果可能会有所不同。
Unicode string is an immutable sequence of Unicode codepoints in Python. Unicode字符串是Python中Unicode代码点的不可变序列。 len(u'\') == 1
: it does not contain uf001
(5 characters) in it. len(u'\') == 1
:其中不包含uf001
(5个字符)。 You could write it as u''
(it is necessary to declare the character encoding of your source file on Python 2 if you use non-ascii characters): 您可以将其写为u''
(如果使用非ascii字符,则必须在Python 2上声明源文件的字符编码):
>>> u'\uf001' == u''
True
It is just a different way to represent exactly the same Unicode character (a single codepoint in this case). 这是表示完全相同的Unicode字符(在这种情况下为单个代码点)的另一种方式。
Note: some user-perceived characters may span several Unicode codepoints eg: 注意:某些用户可感知的字符可能跨越多个 Unicode代码点,例如:
>>> import unicodedata
>>> unicodedata.normalize('NFKD', u'ё')
u'\u0435\u0308'
>>> print(unicodedata.normalize('NFKD', u'ё'))
ё
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.