byte count unicode string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • willie

    #1

    byte count unicode string

    # What's the correct way to get the
    # byte count of a unicode (UTF-8) string?
    # I couldn't find a builtin method
    # and the following is memory inefficient.

    ustr = "example\xC2\x9 D".decode('U TF-8')

    num_chars = len(ustr) # 8

    buf = ustr.encode('UT F-8')

    num_bytes = len(buf) # 9


    # Thanks.

  • John Machin

    #2
    Re: byte count unicode string

    willie wrote:
    # What's the correct way to get the
    # byte count of a unicode (UTF-8) string?
    # I couldn't find a builtin method
    # and the following is memory inefficient.
    >
    ustr = "example\xC2\x9 D".decode('U TF-8')
    >
    num_chars = len(ustr) # 8
    >
    buf = ustr.encode('UT F-8')
    >
    num_bytes = len(buf) # 9
    num_bytes = len("example\xC 2\x9D")

    This produces 9; isn't that what you want?
    If not, please explain, with examples, what you mean by "the
    byte count of a unicode (UTF-8) string".

    HTH,
    John

    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: byte count unicode string

      In <mailman.313.11 58732191.10491. python-list@python.org >, willie wrote:
      # What's the correct way to get the
      # byte count of a unicode (UTF-8) string?
      # I couldn't find a builtin method
      # and the following is memory inefficient.
      >
      ustr = "example\xC2\x9 D".decode('U TF-8')
      >
      num_chars = len(ustr) # 8
      >
      buf = ustr.encode('UT F-8')
      >
      num_bytes = len(buf) # 9
      That is the correct way.

      Ciao,
      Marc 'BlackJack' Rintsch

      Comment

      Working...