简体   繁体   English

蟒蛇。 unicode + variable和u + constant之间的区别?

[英]Python. Difference between unicode+variable and u+constant?

Can someone please tell me how to fix this please. 有人可以告诉我如何解决此问题。

This works: 这有效:

    nOrd = (ord(u'ط'))

But this fails: 但这失败了:

s="‎ط"   
s=unicode(s, 'utf-8')
nOrd = (ord((s)))

The error I get is: 我得到的错误是:

TypeError: ord() expected a character, but string of length 2 found TypeError:ord()需要一个字符,但是找到了长度为2的字符串

Your second s is simply not the same text as the first example: 你的第二个s根本就不是同一个文本作为第一个例子:

>>> u'ط'
u'\u0637'
>>> u'ط'.encode('utf8')
'\xd8\xb7'
>>> s="‎ط"
>>> s
'\xe2\x80\x8e\xd8\xb7'
>>> s.decode('utf8')
u'\u200e\u0637'

You have a U+200E LEFT-TO-RIGHT MARK character in the second example. 在第二个示例中,您有一个U + 200E左至右标记字符。 That makes it two characters, not one. 这使其成为两个字符,而不是一个。

You could remove it by stripping with str.lstrip() or by using str.replace() ; 你可以通过剥离除去它str.lstrip()或使用str.replace() ; the first only removes it from the start, the other from everywhere in the string: 第一个只从头开始删除它,另一个从字符串中的任何地方删除它:

s = s.lstrip(u'\u200e')
# or
s = s.replace(u'\u200e', u'')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python中u''前缀和unicode()有什么区别? - What is the difference between u' ' prefix and unicode() in python? Python unicode字符串文字::'\ u0391'和u'\ u0391'之间的区别是什么 - Python unicode string literals :: what's the difference between '\u0391' and u'\u0391' python utf-8字符支持中以`\\ U`和`\\ u`开头的unicode字符有什么区别 - what is the difference between unicode characters starting from `\U` and `\u` in python utf-8 characters support u'string'和unicode(字符串)之间的区别 - Difference between u'string' and unicode(string) curl -u和python请求之间有区别吗 - is there a difference between curl -u and python requests u'somestring'和unicode('somestring'),python 2.7有什么区别 - What's the difference of u'somestring' and unicode('somestring'), python 2.7 在 GEKKO 中固定变量和使用常量之间的区别 - Difference between fixing a variable and using a constant in GEKKO 如何在 Python 2.7 中打印像“u{variable}”这样的 Unicode? - How to print Unicode like “u{variable}” in Python 2.7? 使用Python列出U + E000–U + F8FF之间的所有unicode代码 - List all unicode codes between U+E000–U+F8FF using Python Unicode空格是否有Python常量? - Is there a Python constant for Unicode whitespace?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM