Decoding if it's not unicode

后端 未结 2 1602
遥遥无期
遥遥无期 2021-02-13 16:07

I want my function to take an argument that could be an unicode object or a utf-8 encoded string. Inside my function, I want to convert the argument to unicode. I have something

2条回答
  •  半阙折子戏
    2021-02-13 16:49

    I'm not aware of any good way to avoid the isinstance check in your function, but maybe someone else will be. I can point out that the two weirdnesses you cite are because you're doing something that doesn't make sense: Trying to decode into Unicode something that's already decoded into Unicode.

    The first should instead look like this, which decodes the UTF-8 encoding of that string into the Unicode version:

    >>> 'cer\xc3\xb3n'.decode('utf-8')
    u'cer\xf3n'
    

    And your second should look like this (not using a u'' Unicode string literal):

    >>> unicode('hello', 'utf-8')
    u'hello'
    

提交回复
热议问题