在Windows上的Python 2.x中从命令行参数读取Unicode字符

在Windows上的Python 2.x中从命令行参数读取Unicode字符,第1张

在Windows上的Python 2.x中从命令行参数读取Unicode字符

这是我要寻找的解决方案,它调用Windows

GetCommandLineArgvW
函数:
在Windows下从ActiveState获取带有Unipre字符的sys.argv

但是我进行了一些更改,以简化其用法并更好地处理某些用法。这是我用的:

win32_unipre_argv.py

"""win32_unipre_argv.pyimporting this will replace sys.argv with a full Unipre form.Windows only.From this site, with adaptations:      http://pre.activestate.com/recipes/572200/Usage: simply import this module into a script. sys.argv is changed tobe a list of Unipre strings."""import sysdef win32_unipre_argv():    """Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unipre    strings.    Versions 2.x of Python don't support Unipre in sys.argv on    Windows, with the underlying Windows API instead replacing multi-byte    characters with '?'.    """    from ctypes import POINTER, byref, cdll, c_int, windll    from ctypes.wintypes import LPCWSTR, LPWSTR    GetCommandLineW = cdll.kernel32.GetCommandLineW    GetCommandLineW.argtypes = []    GetCommandLineW.restype = LPCWSTR    CommandLineToArgvW = windll.shell32.CommandLineToArgvW    CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]    CommandLineToArgvW.restype = POINTER(LPWSTR)    cmd = GetCommandLineW()    argc = c_int(0)    argv = CommandLineToArgvW(cmd, byref(argc))    if argc.value > 0:        # Remove Python executable and commands if present        start = argc.value - len(sys.argv)        return [argv[i] for i in     xrange(start, argc.value)]sys.argv = win32_unipre_argv()

现在,我使用它的方法就是:

import sysimport win32_unipre_argv

从那时起,

sys.argv
是Unipre字符串列表。Python
optparse
模块似乎很高兴解析它,这很棒。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5646531.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存