2014-11-21 88 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