2014-11-21 87 views
2

我的工作(法国)的PC上使用Python 3.4Python的os.popen和Unicode

for line in os.popen('dir'): print(line.rstrip())

我得到的第一行预期

Le volume dans le lecteur C s'appelle SYSTEME 

但第二(与

Lenumérode s rie du volume est C 250-47DD

我得到的错误信息:

return codecs.charmap_encode(input,self.errors,encoding_map)[0] 
UnicodeEncodeError: 'charmap' codec can't encode character '\u201a' in position 7: character maps to 
<undefined> 

我能做些什么? 在此先感谢您的帮助

+0

你应该使用子*,因为不推荐使用2.6版本:此功能是过时*,或只使用OS .listdir。 – 2014-11-21 18:11:11

回答

1

您需要通过正确的编码打印之前编码您行:

for line in os.popen('dir'): 
     print(line.rstrip().encode('UTF-8')) # as utf8 is a universal encoding i use it you can use another too 
+0

太简单了... 非常感谢! – CSJNL 2014-11-21 17:58:32

+0

@CSJNL不客气! – Kasramvd 2014-11-21 18:00:35