python - What does the 'u' symbol mean in front of string values? -
this question has answer here:
- what's u prefix in python string? 7 answers
yes in short know why seeing u in front of keys , values.
i rendering form. form has check-box particular label , 1 text field ip address. creating dictionary keys being label hardcoded in list_key , values dictionary taken form input (list_value). dictionary created preceded u values. here sample output dictionary:
{u'1': {'broadcast': u'on', 'arp': '', 'webserver': '', 'ipaddr': u'', 'dns': ''}} can please explain doing wrong. not getting error when simulate similar method in pyscripter. suggestions improve code welcome. thank you
#!/usr/bin/env python import webapp2 import itertools import cgi form =""" <form method="post"> firewall <br><br> <select name="profiles"> <option value="1">profile 1</option> <option value="2">profile 2</option> <option value="3">profile 3</option> </select> <br><br> check box implement particular policy <br><br> <label> allow broadcast <input type="checkbox" name="broadcast"> </label> <br><br> <label> allow arp <input type="checkbox" name="arp"> </label><br><br> <label> allow web traffic external address internal webserver <input type="checkbox" name="webserver"> </label><br><br> <label> allow dns <input type="checkbox" name="dns"> </label><br><br> <label> block particular internet protocol address <input type="text" name="ipaddr"> </label><br><br> <input type="submit"> </form> """ dictionarymain={} class mainhandler(webapp2.requesthandler): def get(self): self.response.out.write(form) def post(self): # parameters form profile = self.request.get('profiles') broadcast = self.request.get('broadcast') arp = self.request.get('arp') webserver = self.request.get('webserver') dns =self.request.get('dns') ipaddr = self.request.get('ipaddr') # create dictionary above parameters list_value =[ broadcast , arp , webserver , dns, ipaddr ] list_key =['broadcast' , 'arp' , 'webserver' , 'dns' , 'ipaddr' ] #self.response.headers['content-type'] ='text/plain' #self.response.out.write(profile) # map 2 list dictionary using itertools adict = dict(zip(list_key,list_value)) self.response.headers['content-type'] ='text/plain' self.response.out.write(adict) if profile not in dictionarymain: dictionarymain[profile]= {} dictionarymain[profile]= adict #self.response.headers['content-type'] ='text/plain' #self.response.out.write(dictionarymain) def escape_html(s): return cgi.escape(s, quote =true) app = webapp2.wsgiapplication([('/', mainhandler)], debug=true)
the 'u' in front of string values means string has been represented unicode. letters before strings here called "string encoding declarations". unicode way represent more characters normal ascii can manage.
you can convert string unicode multiple ways:
>>> u'foo' u'foo' >>> unicode('foo') u'foo' but real reason represent (translation here):
>>> val = u'Ознакомьтесь с документацией' >>> val u'\u041e\u0437\u043d\u0430\u043a\u043e\u043c\u044c\u0442\u0435\u0441\u044c \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439' >>> print val Ознакомьтесь с документацией for part, shouldn't have errors in treating them different ascii strings in code.
there other symbols see, such "raw" symbol telling string not interpret special characters. extremely useful when doing regular expression in python.
>>> 'foo\"' 'foo"' >>> r'foo\"' 'foo\\"' acsii , unicode strings can logically equivalent:
>>> bird1 = unicode('unladen swallow') >>> bird2 = 'unladen swallow' >>> bird1 == bird2 true
Comments
Post a Comment