为什么会发生以下情况:
>>> u'\u0308'.encode('mbcs') #UMLAUT
'\xa8'
>>> u'\u041A'.encode('mbcs') #CYRILLIC CAPITAL LETTER KA
'?'
>>>
Run Code Online (Sandbox Code Playgroud)
我有一个Python应用程序接受操作系统的文件名.它适用于一些国际用户,但不适用于其他用户.
例如,这个unicode文件名:u'\ u041a\u0433\u044b\u044b\u0448\u0444\u0442'
不会使用Windows的'mbcs'编码进行编码(文件系统使用的编码,由sys.getfilesystemencoding()返回).我得到'???????',表示编码器在这些字符上失败.但这没有任何意义,因为文件名来自用户开始.
更新:这是我背后原因的背景...我的系统上有一个文件,名字是西里尔文.我想用该文件作为参数调用subprocess.Popen().Popen不会处理unicode.通常情况下,我可以使用sys.getfilesystemencoding()给出的编解码器对参数进行编码.在这种情况下,它将无法正常工作
我有一个我想打开的unicode文件名.以下代码:
cmd = u'cmd /c "C:\\Pok\xe9mon.mp3"'
cmd = cmd.encode('utf-8')
subprocess.Popen(cmd)
Run Code Online (Sandbox Code Playgroud)
回报
>>> 'C:\Pok?mon.mp3' is not recognized as an internal or external command, operable program or batch file.
Run Code Online (Sandbox Code Playgroud)
即使该文件确实存在.为什么会这样?