zoukankan      html  css  js  c++  java
  • 解决UnicodeEncodeError。python的docker镜像增加locale 中文支持

    用pandas的pd.read_excel()打开中文名的xlsx,报错,本来以为是xlrd的问题后来发现,是open()函数就报错:

    “UnicodeEncodeError: 'ascii' codec can't encode characters in position 26-30: ordinal not in range(128)”

    但是同样的xlsx在win7下用pd.read_excel()打开正常

    用print(locale.getdefaultlocale()) 检查一下编码:

    编码居然是(None, None) 而win7下是('zh_CN', 'cp936')

    用的docker镜像是python:latest  是基于debian的。

    在dockerfile里增加中文语言支持:

    # 设置中文支持
    RUN apt-get install -y locales locales-all
    
    RUN locale-gen zh_CN.UTF-8 zh_cn && dpkg-reconfigure locales && dpkg-reconfigure locales && locale-gen zh_CN.UTF-8 && /usr/sbin/update-locale LANG=zh_CN.UTF-8
    
    ENV LANG zh_CN.UTF-8  
    ENV LANGUAGE zh_CN:zh  
    ENV LC_ALL zh_CN.UTF-8

    build之后,

    再看,显示('zh_CN', 'UTF-8') 打开xlsx就正常了。

  • 相关阅读:
    P1495 曹冲养猪
    luoguP4281[AHOI2008]紧急集合 / 聚会
    NOIP1997 代数表达式
    luogu P3709大爷的字符串题
    luoguP3912 素数个数
    POJ 1065 Wooden Sticks
    POJ 1063 Flip and Shift
    POJ 1062 昂贵的聘礼
    POJ 1003 Hangover
    POJ 1001 Exponentiation
  • 原文地址:https://www.cnblogs.com/xuanmanstein/p/9100507.html
Copyright © 2011-2022 走看看