Is there a good reason why I shouldn't start all my python programs with this? Is there something special lost when doing exec like this?

#!/usr/bin/python

import os, sys

if sys.stdout.encoding == None:

os.putenv("PYTHONIOENCODING",'UTF-8')

os.execv(sys.executable,['python']+sys.argv)

print sys.stdout.encoding

There are 60 questions about PYTHONIOENCODING so I guess it's a common problem, but in case you don't know, this is done because when sys.stdout.encoding == None then you can only print ascii chars, so e.g. print "åäö" will throw an exception..

EDIT This happens to me when stdout is a pipe; python encoding.py|cat will set encoding to None

Another solution is to change the codec of stdout sys.stdout = codecs.getwriter('utf8')(sys.stdout) which I'm guessing is the correct answer dispite the comments on that question.

解决方案

Yes, there is a good reason not to start all your Python programs like that.

First of all:

sys.stdout.encoding is None if Python doesn't know what encoding the stdout supports. This, in most cases, is because it doesn't really support any encoding at all. In your case it's because the stdout is a file, and not a terminal. But it could be set to None because Python also fails to detect the encoding of the terminal.

Second of all: You set the environment variable and then start a new process with the smae command again. That's pretty ugly.

So, unless you plan to be the only one using your programs, you shouldn't start them like that. But if you do plan to be the only using your program, then go ahead.

More in-depth explanation

A better generic solution under Python 2 is to treat stdout as what it is: An 8-bit interface. And that means that anything you print to to stdout should be 8-bit. You get the error when you are trying to print Unicode data, because print will then try to encode the Unicode data to the encoding of stdout, and if it's None it will assume ASCII, and fail, unless you set PYTHONIOENCODING.

But by printing encoded data, you don't have this problem. The following works perfectly even when the output is piped:

print u'ÅÄÖ'.encode('UTF8')

(However, this will fail Under Python 3, because under Python 3, stdout is no longer 8-bit IO, you are supposed to give it Unicode data, and it will encode by itself. If you give it binary data, it will print the representation. Therefore on Python 3 you don't have this problem in the first place).

python stdout,在Python中的Stdout编码相关推荐

  1. python sys stdout_Python-如何将sys.stdout复制到日志文件?

    编辑:由于似乎没有解决方案,或者我正在做一些人都不知道的非标准操作-我将修订我的问题,并问:当python应用正在制作日志时,完成记录的最佳方法是什么?很多系统调用? 我的应用程序有两种模式.在交互模 ...

  2. 如何解决python中编码错误的问题_【总结】Python 2.x中常见字符编码和解码方面的错误及其解决办法...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 对于Python解析器 而Python解析器所干的事情,就是: Python解析器,根据当前的所用的字符串编码类型 此字符串编码类型,是你自己所设置的 不 ...

  3. python2发送http不编码_[转]Python 2.x中常见字符编码和解码方面的错误及其解决办法...

    Python 2.x中的字符编码,设计的的确不好,导致初学者,甚至是即使用Python很长时间的人,都会经常遇到字符编解码方面的错误. 下面就把一些常见情,尽量的都整理出来,并给出相应的解决办法. 看 ...

  4. python stdin和stdout_stdin似乎比stdout(python)慢得多.为什么?

    我有两个需要相互通信的 python程序(一个是子进程).目前我通过stdin和stdout这样做.然而,写入子进程的标准似乎很慢. a.py,一个采用任意行输入并打印时间的程序: from time ...

  5. python中的URL编码和解码

    python中的URL编码和解码:test.py 1 # 引入urllib的request模块 2 import urllib.request 3 4 url = 'https://www.douba ...

  6. python的chr可以转换中文吗,chr()在python中怎么实现编码的转换

    chr()在python中怎么实现编码的转换 发布时间:2020-12-14 09:36:42 来源:亿速云 阅读:95 作者:小新 这篇文章主要介绍chr()在python中怎么实现编码的转换,文中 ...

  7. python中factor函数_左手用R右手Python系列——因子变量与分类重编码

    原标题:左手用R右手Python系列--因子变量与分类重编码 欢迎关注天善智能 hellobi.com,我们是专注于商业智能BI,大数据,数据分析领域的垂直社区,学习.问答.求职,一站式搞定! 对商业 ...

  8. python 检测文件编码_[常用] 在Python中检测网页编码

    [常用] 在Python中检测网页编码 在使用Python抓取网页并进行分析时出现这个错误: UnicodeDecodeError: 'utf8' codec can't decode byte 0x ...

  9. python url解码_对python中url参数编码与解码的实例详解

    一.简介 在python中url,对于中文等非ascii码字符,需要进行参数的编码与解码. 二.关键代码 1.url编码 对字符串编码用urllib.parse包下的quote(string, saf ...

最新文章

  1. C# 多线程六之Task(任务)三之任务工厂
  2. Java期末复习——ch02基本类型(进制转换,数据类型转换,汉字编码)
  3. 需求: 计算一下你来到这个世界多少天 拓展:改成 生日是键盘录入的 而且可以循环录入 直到录入 exit 结束循环
  4. 【Unity】6.8 Quaternion类(四元数)
  5. Apache Avro
  6. ajax 赋值 获取,ajax得到的数据赋值给js中的全局变量
  7. nparray和tensor的相互转化
  8. SQLAlchemy engine.Connection
  9. BZOJ 2821: 作诗(Poetize) [分块]
  10. 下载——百度文库下载方法
  11. Windows 搭建 翼龙面板 ( Pterodactyl ) 前端 教程
  12. 安泰测试新手教程-泰克数字示波器使用方法
  13. 动软代码生成器-模板修改,模型修改
  14. dvhop三边测量法MATLAB,基于非测距的DV-Hop定位算法改进
  15. Mathpix Snip他们都说好用!我也试一试尝试下载-安装-使用
  16. 使用npm运行react程序报错The 'mode' option has not been set, webpack will fallback to 'production' for th
  17. ThinkPad E531加装固态硬盘全过程
  18. 云计算技术的应用和前景
  19. c++学习笔记(6)
  20. 将本地项目上传至码云仓库

热门文章

  1. linux怎么使用git安装目录,Linux系统中怎么安装Git?
  2. 【Elasticsearch】Elasticsearch日志场景最佳实践
  3. 【java】java 1.8 之 supplier 理解
  4. 【Java】Java之Collections.emptyList()、emptySet()、emptyMap()的作用和好处以及要注意的地方
  5. Spark Structured : java.lang.RuntimeException: Offsets committed out of order: 2 followed by 0
  6. 95-150-045-源码-Sink-Streaming Parquet File
  7. 【Flink】Could not instantiate the executor. Make sure a planner module is on the classpath
  8. Spring :@Inject注解
  9. 【SpringMVC】SpringMVC: @RequestBody 和@ResponseBody 注解详解 NoHandlerFoundException
  10. hadoop: Operation category READ is not supported in state standby