zxCoder
V2EX  ›  问与答

请教一个 Python pandas 数据编码的问题

  •  
  •   zxCoder · Dec 13, 2021 · 943 views
    This topic created in 1635 days ago, the information mentioned may be changed or developed.

    出错代码:

    
    try:
        num_unique = data.nunique()
    except UnicodeEncodeError:
        print(type(data))
        for i,v in data.items():
            print(i,v)
    

    data 是很多个 pandas Series 中的一个

    总是跑到某个 data ,就在这里出错,报错是

    UnicodeEncodeError: 'utf-8' codec can't encode characters in position 41-42: surrogates not allowed

    而且也无法打印出这个 data 的数据,这是为什么呢?不知道怎么解决

    1 replies    2021-12-13 23:45:06 +08:00
    3dwelcome
        1
    3dwelcome  
       Dec 13, 2021
    不懂 py, 但我知道 surrogates 的意思。是指普通 UNICODE 扩展后,四字节的 UNICODE 片区。

    通常范围是 U+D800 - U+DFFF ,可以考虑把数据过滤一次,utf8->unicode 双字节,遍历一次,把范围内的 surrogates 全部干掉就可以了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2826 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:02 · PVG 18:02 · LAX 03:02 · JFK 06:02
    ♥ Do have faith in what you're doing.