代码之家  ›  专栏  ›  技术社区  ›  ibz

什么编码看起来像ASCII码,但在每个字节之前有空字节?

  •  3
  • ibz  · 技术社区  · 15 年前

    s = u'\x00Q\x00u\x00i\x00c\x00k'
    >>> print s
    Quick
    >>>
    >>> s == 'Quick'
    False
    >>>
    >>> import re
    >>> re.search('Quick', s)
    >>>
    >>> import chardet
    >>> chardet.detect(s)
    /usr/lib/pymodules/python2.6/chardet/universaldetector.py:69: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
      if aBuf[:3] == '\xEF\xBB\xBF':
    /usr/lib/pymodules/python2.6/chardet/universaldetector.py:72: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
      elif aBuf[:4] == '\xFF\xFE\x00\x00':
    /usr/lib/pymodules/python2.6/chardet/universaldetector.py:75: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
      elif aBuf[:4] == '\x00\x00\xFE\xFF':
    /usr/lib/pymodules/python2.6/chardet/universaldetector.py:78: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
      elif aBuf[:4] == '\xFE\xFF\x00\x00':
    /usr/lib/pymodules/python2.6/chardet/universaldetector.py:81: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
      elif aBuf[:4] == '\x00\x00\xFF\xFE':
    /usr/lib/pymodules/python2.6/chardet/universaldetector.py:84: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
      elif aBuf[:2] == '\xFF\xFE':
    /usr/lib/pymodules/python2.6/chardet/universaldetector.py:87: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
      elif aBuf[:2] == '\xFE\xFF':
    {'confidence': 1.0, 'encoding': 'ascii'}
    >>>
    >>> chardet.detect(s)
    {'confidence': 1.0, 'encoding': 'ascii'}
    >>> 
    
    2 回复  |  直到 15 年前
        1
  •  8
  •   Delan Azabani    15 年前

    UTF-16大端

        2
  •  2
  •   John Machin Santi    15 年前

    你有UTF-16BE没有BOM。如文档所述,chardet在没有BOM的情况下不会使用UTF nnxE。

    >>> s = '\x00Q\x00u\x00i\x00c\x00k' #### Note: dropping the spurious `u` prefix
    >>> s.decode('utf_16be')
    u'Quick'
    >>>