美文网首页
python基础知识

python基础知识

作者: 长林赤焰 | 来源:发表于2019-03-02 10:18 被阅读0次

    Anaconda操作

    如何查看已经安装的库?
    Anaconda环境下在PowerShell中输入以下代码:

    conda list
    

    字符串的解码和编码

    Python3默认的str字符串编码方式是unicode。
    decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码。
    encode的作用是将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串str2转换成gb2312编码。

    • 纯英文可以用ASCII将str编码为bytes
    • 含有中文可用utf-8将str转化为bytes
    • 从网络或磁盘上读取的字节流为bytes
    >>> s_u = 'sigai'
    >>> s_b = b'sigai'
    >>> type(s_u)
    <class 'str'>
    >>> type(s_b)
    <class 'bytes'>
    >>> type(s_b.decode('ascii'))
    <class 'str'>
    >>> type(s_u.encode('ascii'))
    <class 'bytes'>
    >>> s_u = 'sigai在线编程'
    >>> print(s_u.encode('utf-8'))
    b'sigai\xe5\x9c\xa8\xe7\xba\xbf\xe7\xbc\x96\xe7\xa8\x8b'
    

    相关文章

      网友评论

          本文标题:python基础知识

          本文链接:https://www.haomeiwen.com/subject/klqeuqtx.html