好好百科

分享有趣又有用的知识

用户工具

站点工具


侧边栏

导航菜单

python:index

Python

~/.config/pip/pip.conf

pip.conf
[global]
index-url = https://mirrors.cloud.tencent.com/pypi/simple
trusted-host = mirrors.cloud.tencent.com
 
[list]
format=columns

自制UTF-8编码函数

参见 https://zh.wikipedia.org/wiki/UTF-8

def encode_utf8(string):
    """unicode string 编码为 utf-8"""
    result = b''
    for _value in map(ord, string):
        if _value <= 0x7f:
            result += bytes([_value])
        elif _value <= 0x7ff:
            _ = f'{_value:b}'.zfill(11)
            result += bytes([
                int(f'110{_[:5]}', 2),
                int(f'10{_[5:]}', 2)
            ])
        elif _value <= 0xffff:
            _ = f'{_value:b}'.zfill(16)
            result += bytes([
                int(f'1110{_[:4]}', 2),
                int(f'10{_[4:10]}', 2),
                int(f'10{_[10:]}', 2)
            ])
        else:
            _ = f'{_value:b}'.zfill(21)
            result += bytes([
                int(f'11110{_[:3]}', 2),
                int(f'10{_[3:9]}', 2),
                int(f'10{_[9:15]}', 2),
                int(f'10{_[15:]}', 2)
            ])
    return result

unicode string

import sys
a = 'aæ汉🐍'
b = 'æ'
c = '汉'
d = '🐍'
print(repr(a), sys.getsizeof(a))
print(repr(b), sys.getsizeof(b))
print(repr(c), sys.getsizeof(c))
print(repr(d), sys.getsizeof(d))
'a' 50
'æ' 74
'汉' 76
'🐍' 80
import sys
a = 'aa'
b = 'ææ'
c = '汉汉'
d = '🐍🐍'
print(repr(a), sys.getsizeof(a))
print(repr(b), sys.getsizeof(b))
print(repr(c), sys.getsizeof(c))
print(repr(d), sys.getsizeof(d))
'aa' 51
'ææ' 75
'汉汉' 78
'🐍🐍' 84
python/index.txt · 最后更改: 2019/08/13 23:52 由 老赵