"convert" string to bytes without changing data (encoding)

Dave Angel d at davea.name
Wed Mar 28 19:16:57 CEST 2012


On 03/28/2012 04:56 AM, Peter Daum wrote:
> Hi,
>
> is there any way to convert a string to bytes without
> interpreting the data in any way? Something like:
>
> s='abcde'
> b=bytes(s, "unchanged")
>
> Regards,
>                                Peter


You needed to specify that you are using Python 3.x .  In python 2.x, a 
string is indeed a series of bytes.  But in Python 3.x, you have to be 
much more specific.

For example, if that string is coming from a literal, then you usually 
can convert it back to bytes simply by encoding using the same method as 
the one specified for the source file.  So look at the encoding line at 
the top of the file.



-- 

DaveA




More information about the Python-list mailing list