You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On Windows, if you try to pass text in encoding different than ASCII, Python will replace it with question marks.
test2.py
import sys
with open('data.bin', 'wb') as binfile:
for arg in sys.argv:
binfile.write(arg)
binfile.write(' ')
> python test2.py Русский текст
> type data.bin
E:\test2.py ??????? ?????
The solution is to monkeypatch sys.argv on Windows. Based on this recipe it makes sys.argv to be in utf-8:
defwin32_utf8_argv():
"""Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode strings. Versions 2.x of Python don't support Unicode in sys.argv on Windows, with the underlying Windows API instead replacing multi-byte characters with '?'. """fromctypesimportPOINTER, byref, cdll, c_int, windllfromctypes.wintypesimportLPCWSTR, LPWSTRGetCommandLineW=cdll.kernel32.GetCommandLineWGetCommandLineW.argtypes= []
GetCommandLineW.restype=LPCWSTRCommandLineToArgvW=windll.shell32.CommandLineToArgvWCommandLineToArgvW.argtypes= [LPCWSTR, POINTER(c_int)]
CommandLineToArgvW.restype=POINTER(LPWSTR)
cmd=GetCommandLineW()
argc=c_int(0)
argv=CommandLineToArgvW(cmd, byref(argc))
ifargc.value>0:
# Remove Python executable and commands if presentstart=argc.value-len(sys.argv)
return [argv[i].encode('utf-8') foriinrange(start, argc.value)]
ifsys.platform=="win32":
sys.argv=win32_utf8_argv()
The text was updated successfully, but these errors were encountered:
On Windows, if you try to pass text in encoding different than ASCII, Python will replace it with question marks.
test2.py
The solution is to monkeypatch
sys.argv
on Windows. Based on this recipe it makessys.argv
to be inutf-8
:The text was updated successfully, but these errors were encountered: