Python:.encode(' ascii','忽略')做什么?

时间:2014-01-31 05:08:21

标签: python unicode

我是Python的新手,正在从工作中查看一些代码。

我注意到有很多行包含row[0].encode('ascii', 'ignore')

我做了一些阅读,看起来它正在从unicode转换为字节。

它只是一种将字符串从u'string'转换为字符串的方法吗?

1 个答案:

答案 0 :(得分:3)

    encode(...)
        S.encode([encoding[,errors]]) -> object

        Encodes S using the codec registered for encoding. encoding defaults
        to the default encoding. errors may be given to set a different error
        handling scheme. Default is 'strict' meaning that encoding errors raise
        a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
        'xmlcharrefreplace' as well as any other name registered with
        codecs.register_error that is able to handle UnicodeEncodeErrors.

因此它将asicode字符串编码为ascii并忽略错误

>>> "hello\xffworld".encode("ascii")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character '\xff' in position 5: ordinal not in range(128)

VS

>>> "hello\xffworld".encode("ascii", "ignore")
b'helloworld'