最近使用you-get這個工具下載視頻,發現命令行窗口里顯示的媒體標題是亂碼(但文件管理器里顯示正常)。我的命令行窗口的code page是936,sys.stdout.encoding是utf-8,sys.getdefaultencoding()是utf8。
查了不少資料,都不行。csdn上查到的,都是說加一句
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
就搞定,但是試了,卻提示:
ValueError: I/O operation on closed file
有人說也可用'中文'.encode('utf-8').decode(sys.stdout.encoding),也不行。
又查了不少資料,最后還是在Stack Overflow上找到了正常工作的代碼:
sys.stdout.buffer.write(("title: %s" % self.title).encode('gbk'))
sys.stdout.flush()