How to port bytes formatting to Python 3.x ?

Stefan Behnel stefan_ml at behnel.de
Sat Jan 1 07:08:48 EST 2011


Baptiste Lepilleur, 01.01.2011 12:53:
> 2011/1/1 Stefan Behnel
>> Baptiste Lepilleur, 01.01.2011 10:01:
>>> I'm trying to port a small library to Python 3.x, and I'm wondering what
>>> is the best way to port statements such as the one belows that are
>>> frequently found in network protocol implementation:
>>> ...
>>>
>> See my answer in the other thread you started on this topic. You need to
>> wrap the literal in a function call that converts it to a bytes literal when
>> running in Python 3.
>
> Is there a robust implementation of the format operator % for bytes that can
> substitute %s?

Concatenation is portable and seems to suite your examples (which you 
stripped above). For more involved cases (as are also likely to occur in 
network protocol code), have a look at the struct module.

http://docs.python.org/py3k/library/struct.html

Stefan




More information about the Python-list mailing list