How can I determine the byte length of a utf-8 encoded string in Python?

前端 未结 3 638
自闭症患者
自闭症患者 2020-12-29 23:33

I am working with Amazon S3 uploads and am having trouble with key names being too long. S3 limits the length of the key by bytes, not characters.

From the docs:

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 23:42

    Use the string 'encode' method to convert from a character-string to a byte-string, then use len() like normal:

    >>> s = u"¡Hola, mundo!"                                                      
    >>> len(s)                                                                    
    13 # characters                                                                             
    >>> len(s.encode('utf-8'))   
    14 # bytes
    

提交回复
热议问题