Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace element of a list by appending escape character to each element

how to replace '1c' to '\x1c' in python. I have a list with elements like '12','13' etc and want to replace with '\x12', '\x13' etc.

here is what i tried and failed

 letters=[]
 for i in range(10,128,1):
      a=(str(hex(i))).replace('0x','\x')
      letters.append(a)
      print letters

I need is '31' to be replaced by '\x31' ---> '1' not '\x31' 0r \x31

like image 539
ceasif Avatar asked Dec 20 '25 12:12

ceasif


1 Answers

You need to use the built-in function chr to return the correct ascii code (which is the string you are after):

>>> [chr(i) for i in range(10,20,1)]
['\n', '\x0b', '\x0c', '\r', '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13']
like image 56
Burhan Khalid Avatar answered Dec 23 '25 02:12

Burhan Khalid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!