Skip to main content
 首页 » 编程设计

Python os.popen 和 unicode

2025年05月04日11qlqwjy

我正在使用 Python 3.4 开发(法语)PC

对于 os.popen('dir') 中的行: 打印(线。rstrip())

我得到了预期的第一行

Le volume dans le lecteur C s'appelle SYSTEME 

但对于第二个(使用é)

Le numéro de série du volume est C250-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> 

我能做什么? 预先感谢您的帮助

请您参考如下方法:

您需要在打印前通过适当的编码对您的行进行编码:

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