解决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就正常了。

 

转载于:https://www.cnblogs.com/xuanmanstein/p/9100507.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值