2012-07-01 92 views
88

簡而言之,我想知道爲什麼我在我的鑰匙和價值觀前面看到了你。'u'符號在字符串值前面意味着什麼?

我正在渲染一個表單。該表格具有特定標籤的複選框和IP地址的一個文本字段。我正在創建一個字典,其中鍵是在list_key中硬編碼的標籤,字典的值是從表單輸入(list_value)中獲取的。字典已創建,但在某些值的前面加上了u。這裏是字典的輸出示例:

{u'1': {'broadcast': u'on', 'arp': '', 'webserver': '', 'ipaddr': u'', 'dns': ''}} 

有人可以請解釋我做錯了什麼。當我在pyscripter中模擬類似的方法時,我沒有收到錯誤。歡迎任何改進代碼的建議。謝謝

#!/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 the box to implement the 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 from external address to 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): 
     # get the parameters from the 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 a dictionary for the 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 two list to a 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) 
+1

是您實際的問題:「爲什麼我看到在我的鑰匙和價值觀面前'u'」? – jdi

+0

你不會在任何地方發現你首先遇到錯誤。 – jdi

+3

這是因爲他們是Unicode字符串: http://stackoverflow.com/questions/599625/python-string-prints-as-ustring – user

回答

121

字符串值前的'u'表示該字符串已被表示爲unicode。字符串之前的字母稱爲「字符串編碼聲明」。 Unicode是一種代表比ascii可以管理的更多字符的方式。

您可以將字符串轉換爲Unicode多種方式:

>>> u'foo' 
u'foo' 
>>> unicode('foo') 
u'foo' 

但真正的原因是爲了表示這樣的事情(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 
Ознакомьтесь с документацией 

在大多數情況下,你不應該」在這段代碼中,對待它們與ascii字符串不同的地方有任何錯誤。

您還會看到其他符號,例如告訴字符串不能解釋任何特殊字符的「原始」符號。這在python中執行正則表達式時非常有用。

>>> 'foo\"' 
'foo"' 
>>> r'foo\"' 
'foo\\"' 

ACSII和Unicode字符串可以是邏輯上等同:

>>> bird1 = unicode('unladen swallow') 
>>> bird2 = 'unladen swallow' 
>>> bird1 == bird2 
True 
+0

謝謝..只是要說清楚,我明白我不會獲取字符串表示爲unicode的錯誤。 – user1488987

+0

@ user1488987:正確。你可以在你的字典中使用unicode – jdi

+3

@jdi,很好的示例字符串:)) –

相關問題