UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

在程序中调用封装的python的exe时,重定向exe的输出时,出现错误:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

在命令行方式下运行这个exe,正确输出。

在命令行方式下运行exe,>d:\temp.txt下,出现UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

百思不得其解,应该是pyhon的默认编码,与系统编码冲突。

在网上搜了一个解决方案,修改python安装目录下的lib中的site.py  文件。

def setencoding():
    """Set the string encoding used by the Unicode implementation.  The
    default is 'ascii', but if you're willing to experiment, you can
    change this."""
    encoding = "ascii" # Default value set by _PyUnicode_Init()
    if 0:
        # Enable to support locale aware default string encodings.
        import locale
        loc = locale.getdefaultlocale()
        if loc[1]:
            encoding = loc[1]
    if 0:
        # Enable to switch off string to Unicode coercion and implicit
        # Unicode to string conversion.
        encoding = "undefined"
    if encoding != "ascii":
        # On Non-Unicode builds this will raise an AttributeError...
        sys.setdefaultencoding(encoding) # Needs Python Unicode build !

把 此处的 第一个if 0:改为 if 1:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值