2014-12-22 128 views
1

我需要幫助。有沒有選擇如何禁用隱式轉換特殊字符? 我的意思是:PYTHON - 將特殊字符轉換爲字符串

#this is normal way with implicit convert special character 
value = '\x55' 
print value 
>>> U 
#and I would like to work with this value like with a string 
print value 
>>> \x55 
+0

東西像'print r'\ x55''? –

+0

@ŠimonKostolný:你可以接受答案,以便將問題標記爲已解決? – MERose

回答

2

您可能需要使用原始字符串:

value = r'\x55' 
0

格式ord字符與{:x}格式。

>>> print '\\x{:x}'.format(ord(value)) 
\x55 
0

是的,但有一個問題。例如,當任何網站上的用戶(在任何博客上)都將該字符串輸入到輸入字段。

<input name='msg' type='text' value='\x55'> 

如何將這個值編碼爲原料「\ X55」 我的意思是,任何特殊字符,只是不完全六角

有來自web.py框架,整個代碼

import web 
ulrs = ('/reg', Reg) 

class Reg: 
    def POST(self): 
     request = web.input(msg=None) 
     return request.msg 
>>> U