Is there a formatted byte string literal in Python 3.6+?

前端 未结 5 1283
走了就别回头了
走了就别回头了 2020-12-17 07:29

I\'m looking for a formatted byte string literal. Specifically, something equivalent to

name = \"Hello\"
bytes(f\"Some format string {name}\")
相关标签:
5条回答
  • 2020-12-17 08:03

    No. The idea is explicitly dismissed in the PEP:

    For the same reason that we don't support bytes.format(), you may not combine 'f' with 'b' string literals. The primary problem is that an object's __format__() method may return Unicode data that is not compatible with a bytes string.

    Binary f-strings would first require a solution for bytes.format(). This idea has been proposed in the past, most recently in PEP 461. The discussions of such a feature usually suggest either

    • adding a method such as __bformat__() so an object can control how it is converted to bytes, or

    • having bytes.format() not be as general purpose or extensible as str.format().

    Both of these remain as options in the future, if such functionality is desired.

    0 讨论(0)
  • 2020-12-17 08:08

    From python 3.6.2 this percent formatting for bytes works for some use cases:

    print(b"Some stuff %a. Some other stuff" % my_byte_or_unicode_string)

    But as AXO commented:

    This is not the same. %a (or %r) will give the representation of the string, not the string iteself. For example b'%a' % b'bytes' will give b"b'bytes'", not b'bytes'.

    Which may or may not matter depending on if you need to just present the formatted byte_or_unicode_string in a UI or if you potentially need to do further manipulation.

    0 讨论(0)
  • 2020-12-17 08:12

    In 3.6+ you can do:

    >>> a = 123
    >>> f'{a}'.encode()
    b'123'
    
    0 讨论(0)
  • 2020-12-17 08:15

    This was one of the bigger changes made from python 2 to python3. They handle unicode and strings differently.

    This s how you'd convert to bytes.

    string = "some string format"
    string.encode()
    print(string)
    

    This is how you'd decode to string.

    string.decode()
    

    I had a better appreciation for the difference between Python 2 versus 3 change to unicode through this coursera lecture by Charles Severence. You can watch the entire 17 minute video or fast forward to somewhere around 10:30 if you want to get to the differences between python 2 and 3 and how they handle characters and specifically unicode.

    I understand your actual question is how you could format a string that has both strings and bytes.

    inBytes = b"testing"
    inString = 'Hello'
    type(inString) #This will yield <class 'str'>
    type(inBytes) #this will yield <class 'bytes'>
    

    Here you could see that I have a string a variable and a bytes variable.

    This is how you would combine a byte and string into one string.

    formattedString=(inString + ' ' + inBytes.encode())
    
    0 讨论(0)
  • 2020-12-17 08:19

    I managed to format a literal in order to send it through a socket:

    msgIndex = 0
    while(IsSomeCondition()):
      msgIndex += 1
      sock.sendto(
        b"msg number %s" % bytes(str(msgIndex), encoding="utf8"),
        (receiverIP, receiverPort)
      )
    # --> b'msg number 1'
    # --> b'msg number 2'
    # --> b'msg number 3'
    

    I'm using Python 3.8.5.

    0 讨论(0)
提交回复
热议问题