import os
for i in os.listdir("E:\Torchlight II"):
print i
This means that the python console app can't write the given character to the console's encoding. More specifically, the python console app created a _io.TextIOWrapperd instance with an encoding that cannot represent the given character. sys.stdout --> _io.TextIOWrapperd --> (your console)
Python determines the encoding of stdout and stderr based on the value of the LC_CTYPE variable, but only if the stdout is a tty. So if I just output to the terminal, LC_CTYPE (or LC_ALL) define the encoding. However, when the output is piped to a file or to a different process, the encoding is not defined, and defaults to 7-bit ASCII.
1). When Python finds its output attached to a terminal, it sets the sys.stdout.encoding attribute to the terminal's encoding. The print statement's handler will automatically encode unicode arguments into str output. 2). When Python does not detect the desired character set of the output, it sets sys.stdout.encoding to None, and print will invoke the "ascii" codec.
import locale print locale.getdefaultlocale()[1]
1). console不能正常显示中文,console的编码是由操作系统决定的(windows环境下); 2). 我的操作系统是win7中文版(GBK),enc = locale.getdefaultlocale()[1]; 3). console的编码决定了sys.stdout.encoding的取值,sys.stdout.encoding = utf-8; 4). 从操作系统枚举目录(E:\Torchlight II)列表返回的字符串也是GBK编码
import os
for i in os.listdir("E:\Torchlight II"):
print i.decode('gbk')
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有