[Python-au] String Casting

sosman sourceforge at metrak.com
Fri Mar 25 02:13:37 CET 2005


Bryce Macdonald wrote:
> I have a string that is posted to my python program (which I cant 
> control) that contains the literal *r’\x7E\x0D\x0A’* and I want it to 
> convert it to equal *‘~\r\n’*
> 
> Is there a way to convert this string ?

Usually more than one way ...

string.join(map(lambda(h): chr(int(h, 16)), s.split('\\x')[1:]), '')

or
string.join([chr(int(h, 16)) for h in s.split('\\x')[1:]], '')
for later versions of python.



More information about the python-au mailing list