Python--字符串

@、字符串中的特殊编码转码

import html
from urllib import parse


def test_unicode():
    # data = r'{"status":-5,"msg":"\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\uff0c\u8fd8\u52696\u6b21\u673a\u4f1a\uff0c\u5982\u5fd8\u8bb0\u5bc6\u7801\u8054\u7cfb\u5ba2\u670d\u5904\u7406","data":[]}'
    # 下面字符串内容是:用户名或密码错误,还剩6次机会,如忘记密码联系客服处理
    data = r'\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef\uff0c\u8fd8\u52696\u6b21\u673a\u4f1a\uff0c\u5982\u5fd8\u8bb0\u5bc6\u7801\u8054\u7cfb\u5ba2\u670d\u5904\u7406'
    # 如果是一个json格式的字符串,可以用json.loads
    # print(json.loads(data))
    print(data.encode().decode("unicode_escape"))


def test_unescape():
    data = "<title>&#20135;&#21697;&#30331;&#35760;&#20449;&#24687;</title>"
    source = html.unescape(data)
    print(source)
    print(html.escape(source))
    ''' 输出结果
        <title>产品登记信息</title>
        &lt;title&gt;产品登记信息&lt;/title&gt;
    '''


def test_quote():
    data = "你好"
    sign = parse.quote(data)
    print(sign)
    print(parse.unquote(sign))
    ''' 输出结果
        %E4%BD%A0%E5%A5%BD
        你好
    '''


test_unicode()
test_unescape()
test_quote()

''' 输出结果
用户名或密码错误,还剩6次机会,如忘记密码联系客服处理
<title>产品登记信息</title>
&lt;title&gt;产品登记信息&lt;/title&gt;
%E4%BD%A0%E5%A5%BD
你好
'''
View Code

 @、关于&#开头的编码说明

原文:https://blog.csdn.net/zt3032/article/details/80850381

关键词:NCR(numeric character reference, 数字字符引用)

有了数字字符引用,就可以在网页中显示Unicode字符了,不用考虑html文件本身的编码,因为数字字符引用只用到ASCII字符集里的字符。所以,即使在gb2312编码的网页中,也可以用NCR显示埃及的象形文字了。

NCR编码是由一个与号(&)跟着一个井号(#), 然后跟着这个字符的Unicode编码值, 最后跟着一个分号组成的, 如:
    &#nnnn; 或者 &#xhhhh;
其中, nnnn是字符编码的十进制表示, 而hhhh是字符的16进制表示.
另外要注意的是x在xml中必须是小写的.而hhhh可以大小写混用, 另外nnnn和hhhh也可以有前导零。
 
@、可以直接按字面连接
只是字面量拼接,不能用于字符串变量直接拼接
print("123"
'456'
'789')
# 结果:123456789

 

@、原始字符串 r

# print(r'c:\a\b\')  # 字符串最后一个字符不能是\,否则报错:SyntaxError: EOL while scanning string literal
print(r'c:\a\b' '\\')  # 最后一个\可使用独立的字符串

 

@、str与repr

str是一个类,以用户友好的方式输出字符串

repr是一个内置函数,以程序员友好的方式输出字符串

s = "hello, \nworld"
print(str(s))
print(repr(s))

''' 结果
hello, 
world
'hello, \nworld'
'''

 

 

 

 

 

 

 

posted @ 2021-09-26 13:40  liDB  阅读(76)  评论(0编辑  收藏  举报