Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何在变量中比较unicode和unicode_Python_Unicode_Comparison - Fatal编程技术网

Python:如何在变量中比较unicode和unicode

Python:如何在变量中比较unicode和unicode,python,unicode,comparison,Python,Unicode,Comparison,已解决 我解决了这个问题,谢谢你的时间 首先,这些是要求: 比较必须在变量范围内。(比较包含unicode的两个变量) Python的版本必须是2.x,我知道版本3已经解决了这个问题,但不幸的是我无法使用它 您好,我有一个用python编码的机器人,我想让它比较两个非英语字母(unicode) 我的问题是,字母必须在变量内,所以我不能使用: “信” 我想比较的两个字母必须在变量范围内 我试过: 字母1==字母2 它显示了这个错误: E:\bots\KiDo\KiDo.py:23:UnicodeW

已解决

我解决了这个问题,谢谢你的时间

首先,这些是要求:

  • 比较必须在变量范围内。(比较包含unicode的两个变量)
  • Python的版本必须是2.x,我知道版本3已经解决了这个问题,但不幸的是我无法使用它
  • 您好,我有一个用python编码的机器人,我想让它比较两个非英语字母(unicode)

    我的问题是,字母必须在变量内,所以我不能使用:

    “信”

    我想比较的两个字母必须在变量范围内

    我试过:

    字母1==字母2

    它显示了这个错误: E:\bots\KiDo\KiDo.py:23:UnicodeWarning:Unicode相等比较无法将两个参数转换为Unicode-将它们解释为不相等 导入系统

    即使两个字母相同,也始终返回False。 所以我猜这意味着我在比较两个unicode字母。

    并尝试:

    字母=unicode(字母)

    但它显示了这个错误:

    UnicodeDecodeError: 'ascii' codec can't decode byte 0xd9 in position 0: ordinal not in range(128)
    
    我在Google上搜索过,但我能找到的只是使用u“”,但这对变量不起作用

    多谢各位

    比较代码:

    word1 = parameters.split()[0]
    word2 = parameters.split()[1]
    word3 = parameters.split()[2]
    word4 = parameters.split()[3]
    word5 = parameters.split()[4]
    if word1[0] == letter:
        if word2[0] == letter:
            if word3[0] == letter:
                if word4[0] == letter:
                    if word5[0] == letter:
                        reply(type, source,u'True')
    

    如果需要比较单个字母,您可以始终使用
    ord(a)==ord(b)
    比较实际值

    针对发布的示例:

    >>> def check(b):
    ...    a = u'ي'
    ...    return (b==a, ord(a), ord(b), ord(a)==ord(b))
    ... 
    >>> check(u'ي')
    (True, 1610, 1610, True)
    >>> 
    

    您确实需要在将unicode标记为unicode时保持一致,即将
    u
    放在引号之前。

    这是我对您所说的任何事情的尝试基础:

    >>> b=u'letter'
    >>> a=u'letter'
    >>> a==b
    True
    >>> a=u'letter2'
    >>> a==b
    False
    
    所以我确信你的变量有问题!我建议在你比较它们之前,试着把它们打印出来!查看变量下的内容

    看,字母ç(ASCII中未显示的字符)可能表示为str对象或unicode对象(可能您对unicode的含义有点困惑)

    此外,如果您试图创建ASCII表中不存在的unicode对象,则必须传递另一个编码表:

    unicode('ç')
    
    这将引发UnicodeDecodeError,因为'ç'不是ASCII格式,而是

    unicode('ç', encoding='utf-8')
    
    将起作用,因为“ç”在UTF-8编码表中显示(正如您的阿拉伯字母可能显示的那样)

    可以将unicode对象与unicode对象进行比较,就像将str对象与str对象进行比较一样,所有这些都必须正常工作

    此外,您可以将str对象与unicode对象进行比较,但如果您比较的不是ASCII字符:“ç”作为str是“\xc3\xa7”,而作为unicode,它只是“\xe7”(在比较中返回False),则这很容易出错

    所以@Karsa可能真的是对的。问题在于“变量”(在Python中,更好的词是对象)。您必须证明您只是在比较str或unicode对象

    因此,更好的代码可能是:

    #-*- coding: utf-8 -*-
    
    def compare_first_letter(phrase, compare_letter):
        # making all unicode objects, with utf-8 codec
        compare_letter = unicode(compare_letter,encoding='utf-8')
        phrase = unicode(phrase,encoding='utf-8')
        # taking the first letters of each word in phrase
        first_letters = [word[0] for word in phrase.split()]
        # comparing the  first letters with the letter you want
        for letter in first_letters:
            if letter != compare_letter:
                return False
        return True # or your reply function
    
    letter = 'ç'
    phrase_1 = "one two three four"
    phrase_2 = "çarinha çapoca çamuca"
    
    print(compare_first_letter(phrase_1,letter))
    print(compare_first_letter(phrase_2,letter))
    

    我想你不明白Unicode和编码的区别

    请参阅本文:

    请注意以下几点。。。UTF-8是Unicode编码,但不是Unicode。下面源文件顶部的
    #coding:utf-8
    声明声明了保存在磁盘上的源文件的编码
    a=u‘ç’
    声明一个Unicode变量
    b='ç'
    是源代码(utf-8)中的一个字节字符串

    请注意,
    repr
    显示字符串的不同类似源代码的表示形式,这样您就可以分辨出不同之处<代码>类型表示对象类型

    # coding: utf-8
    a = u'ç'
    b = 'ç'
    
    print a
    print b
    print repr(a)
    print repr(b)
    print type(a)
    print type(b)
    print a==b                  # Not comparing same types.
    print a==b.decode('utf8')   # Comparing both as Unicode strings.
    print a.encode('utf8')==b   # Comparing both as byte strings.
    
    a
    b
    打印相同,但不相同:

    ç
    ç
    u'\xe7'
    '\xc3\xa7'
    <type 'unicode'>
    <type 'str'>
    C:\Users\metolone\Desktop\Script1.py:11: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
      print a==b
    False
    True
    True
    
    从美国Windows控制台输出。请注意,不同的控制台有不同的编码。Linux通常是UTF-8。非美国Windows控制台可以不同

    word_list and type: [u'\ufeffadi\xf3s', u'ping\xfcino'] <type 'unicode'>
    My terminal encoding: cp437
    Word? pingüino
    word, content and type: pingüino 'ping\x81ino' <type 'str'>
    converted word, content and type: pingüino u'ping\xfcino' <type 'unicode'>
    Comparison: True
    
    word\u列表和类型:[u'\ufeffadi\xf3s',u'ping\xfcino']
    我的终端编码:cp437
    单词平野
    单词、内容和类型:pingüino“ping\x81ino”
    转换后的单词、内容和类型:pingüino u'ping\xfcino'
    比较:正确
    
    我不确定我是否完全遵守了,但也许这个库可以帮助您确定使用
    letter1==letter2
    时总是显示false吗?它们真的是一样的吗?所使用的编码是什么?看起来可能两边都没有unicode字符串。使用打印(键入(letter1))或其他方式获取更多信息。请记住,来自外部世界的UTF-8必须先被解码-text.decode('UTF-8'),然后才能成为unicode。此外,在终端和编辑器之间粘贴的代码可能采用不可预测的编码,因此请严格使用type()进行检查。最后,您的输出需要放回UTF8,比如说print(text.encode('utf-8'))来输出它。请尽量避免python2程序中的UTF-8文本松动,这会造成麻烦。@PadraicCunningham很抱歉,此库与所有其他解决方案相同,它不处理变量,我要做的是列出一个列表,随机选择一个项目,将其放入变量中,然后将其与unicode进行比较。事实上,我正在尝试比较单个字母。但它显示了这个错误:>TypeError:ord()需要一个字符,但找到了长度为2的字符串。@KiDo可能发生这种情况是因为您比较的是str对象而不是unicode对象。@PabloPalácios我比较的是两个阿拉伯文字母,据我所知,它们被认为是unicode。-1:不起作用。我试图比较
    ord(c)==ord(u')
    ,当
    c
    u')时,它返回了
    False
    Python 2.7.6(默认值,2014年3月22日,22:59:56)[GCC 4.8.2],有关更多信息,请键入“帮助”、“版权”、“信用”或“许可证”。>>c=u''>>>ord(c)34>>>ord(u'''')34>>>ord(u'''')==ord(c)True>>>>
    我打印了所有的变量,它们都是一样的,所以基本上,它必须返回True,但比较肯定有问题,这就是为什么它返回False。如果将Unicode打印到控制台,Python会自动返回
    word_list and type: [u'\ufeffadi\xf3s', u'ping\xfcino'] <type 'unicode'>
    My terminal encoding: cp437
    Word? pingüino
    word, content and type: pingüino 'ping\x81ino' <type 'str'>
    converted word, content and type: pingüino u'ping\xfcino' <type 'unicode'>
    Comparison: True