Programming

문자열 값 앞의 'u'기호는 무엇을 의미합니까?

procodes 2020. 7. 27. 21:33
반응형

문자열 값 앞의 'u'기호는 무엇을 의미합니까? [복제]


이 질문에는 이미 답변이 있습니다.

간단히 말해서, 왜 내 키와 값 앞에서 au가 보이는지 알고 싶습니다.

양식을 렌더링 중입니다. 양식에는 특정 레이블에 대한 확인란과 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)

The 'u' in front of the string values means the string has been represented as unicode. Letters before strings here are called "String Encoding declarations". Unicode is a way to represent more characters than normal ascii can manage.

You can convert a string to unicode multiple ways:

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

But the real reason is to represent something like this (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 the most part, you shouldn't have any errors in treating them different than ascii strings in this code.

There are other symbols you will see, such as the "raw" symbol for telling a string not to interpret any special characters. This is extremely useful when doing regular expression in python.

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

ASCII and Unicode strings can be logically equivalent:

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

This is a feature, not a bug.

See http://docs.python.org/howto/unicode.html, specifically the 'unicode type' section.

참고URL : https://stackoverflow.com/questions/11279331/what-does-the-u-symbol-mean-in-front-of-string-values

반응형