栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在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
模块似乎很高兴解析它,这很棒。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/640725.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号