135

はい、要するに、キーと値の前に au が表示される理由を知りたいです。

フォームをレンダリングしています。フォームには、特定のラベル用のチェックボックスと、IP アドレス用の 1 つのテキスト フィールドがあります。キーが 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)
4

2 に答える 2

191

文字列値の前の「u」は、文字列が Unicode 文字列であることを意味します。Unicode は、通常の ASCII よりも多くの文字を表現する方法です。Python 2 を使用しているuということは、Python 3 ではデフォルトで文字列が Unicode になっていることを意味しますが、Python 2 では、u先頭の で Unicode 文字列が区別されます。この回答の残りの部分では、Python 2 に焦点を当てます。

Unicode 文字列は複数の方法で作成できます。

>>> u'foo'
u'foo'
>>> unicode('foo') # Python 2 only
u'foo'

しかし、本当の理由は、次のようなものを表すことです (翻訳はこちら):

>>> 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
Ознакомьтесь с документацией

ほとんどの場合、Unicode 文字列と非 Unicode 文字列は Python 2 で相互運用できます。

rバックスラッシュを解釈しないように文字列に指示する「生の」記号など、他にも表示される記号があります。これは、正規表現を書くのに非常に便利です。

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

Python 2 では、Unicode 文字列と非 Unicode 文字列は等しくなる可能性があります。

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

ただし、Python 3 ではありません:

>>> x = u'asdf' # Python 3
>>> y = b'asdf' # b indicates bytestring
>>> x == y
False
于 2012-07-01T03:52:23.277 に答える
12

これは機能であり、バグではありません。

http://docs.python.org/howto/unicode.html、特に 'unicode type' セクションを参照してください。

于 2012-07-01T03:44:20.407 に答える