Có trong ngắn hạn tôi muốn biết tại sao tôi nhìn thấy một u ở phía trước của các phím và giá trị của tôi.Biểu tượng 'u' có nghĩa là gì trước các giá trị chuỗi?
Tôi đang hiển thị biểu mẫu. Biểu mẫu có hộp kiểm cho nhãn cụ thể và một trường văn bản cho địa chỉ IP. Tôi đang tạo một từ điển với các phím là nhãn được mã hóa cứng trong list_key và các giá trị cho từ điển được lấy từ đầu vào biểu mẫu (list_value). Từ điển được tạo nhưng nó được bắt đầu bằng u cho một số giá trị. đây là đầu ra mẫu cho từ điển:
{u'1': {'broadcast': u'on', 'arp': '', 'webserver': '', 'ipaddr': u'', 'dns': ''}}
ai đó có thể giải thích những gì tôi đang làm sai. Tôi không nhận được lỗi khi tôi mô phỏng phương pháp tương tự trong pyscripter. Bất kỳ đề xuất nào để cải thiện mã đều được chào đón. Cảm ơn bạn
#!/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)
Là câu hỏi thực tế của bạn "Tại sao tôi lại thấy một 'u' trước khóa và giá trị của tôi"? – jdi
Và bạn không hiển thị bất cứ nơi nào mà bạn đang nhận được một lỗi ở nơi đầu tiên. – jdi
Đó là bởi vì chúng là các chuỗi unicode: http://stackoverflow.com/questions/599625/python-string-prints-as-ustring – user