在这里,我们将给大家分享关于在Windows上的Python2.x中从命令行参数读取Unicode字符的知识,让您更了解python读取命令行输入的本质,同时也会涉及到如何更有效地powershell
在这里,我们将给大家分享关于在Windows上的Python 2.x中从命令行参数读取Unicode字符的知识,让您更了解python 读取命令行输入的本质,同时也会涉及到如何更有效地powershell – 如何在Windows 10中从命令行打开/关闭Windows功能?、Python sys.maxint,Linux和Windows上的sys.maxunicode、python unicode rendering:如何知道字体中是否缺少unicode字符、Python命令行参数(Windows)的内容。
本文目录一览:- 在Windows上的Python 2.x中从命令行参数读取Unicode字符(python 读取命令行输入)
- powershell – 如何在Windows 10中从命令行打开/关闭Windows功能?
- Python sys.maxint,Linux和Windows上的sys.maxunicode
- python unicode rendering:如何知道字体中是否缺少unicode字符
- Python命令行参数(Windows)
在Windows上的Python 2.x中从命令行参数读取Unicode字符(python 读取命令行输入)
我希望我的Python脚本能够在Windows中读取Unicode命令行参数。但是看来sys.argv是用某种本地编码而不是Unicode编码的字符串。如何阅读完整Unicode的命令行?
示例代码: argv.py
import sysfirst_arg = sys.argv[1]print first_argprint type(first_arg)print first_arg.encode("hex")print open(first_arg)
在为日语代码页设置的PC上,我得到:
C:\temp>argv.py "PC・ソフト申請書08.09.24.doc"PC・ソフト申請書08.09.24.doc<type ''str''>50438145835c83748367905c90bf8f9130382e30392e32342e646f63<open file ''PC・ソフト申請書08.09.24.doc'', mode ''r'' at 0x00917D90>
我相信,这是Shift-JIS编码的,它对于该文件名“有效”。但是,如果文件名中包含的字符不在Shift-JIS字符集中,则会中断该文件名-
最终的“打开”调用将失败:
C:\temp>argv.py Jörgen.txtJorgen.txt<type ''str''>4a6f7267656e2e747874Traceback (most recent call last): File "C:\temp\argv.py", line 7,in <module> print open(first_arg)IOError: [Errno 2] No such file or directory: ''Jorgen.txt''
注意-我说的是Python 2.x,而不是Python 3.0。我发现Python
3.0提供sys.argv
了适当的Unicode。但是过渡到Python 3.0还为时过早(由于缺乏对第三方库的支持)。
更新:
有几个回答说我应该根据sys.argv
编码的内容进行解码。问题在于它不是完整的Unicode,因此某些字符无法表示。
这是让我感到悲伤的用例:我已在Windows资源管理器中将文件拖放到.py文件中。我的文件名带有各种字符,包括某些不在系统默认代码页中的字符。当在当前代码页编码中无法表示字符时,在所有情况下,我的Python脚本都无法通过sys.argv传递正确的Unicode文件名。
当然,有一些Windows API可以读取具有完整Unicode的命令行(Python 3.0可以做到)。我假设Python 2.x解释器未使用它。
答案1
小编典典这是我要寻找的解决方案,它调用WindowsGetCommandLineArgvW
函数:
在Windows下从ActiveState获取带有Unicode字符的sys.argv
但是我进行了一些更改,以简化其用法并更好地处理某些用法。这是我用的:
win32_unicode_argv.py
"""win32_unicode_argv.pyImporting this will replace sys.argv with a full Unicode form.Windows only.From this site, with adaptations: http://code.activestate.com/recipes/572200/Usage: simply import this module into a script. sys.argv is changed tobe a list of Unicode strings."""import sysdef win32_unicode_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 ''?''. """ 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_unicode_argv()
现在,我使用它的方法就是:
import sysimport win32_unicode_argv
从那时起,sys.argv
是Unicode字符串列表。Pythonoptparse
模块似乎很高兴解析它,这很棒。
powershell – 如何在Windows 10中从命令行打开/关闭Windows功能?
怎么能实现这一目标?
dism /online /Get-Features
这将显示功能名称,因为它们并不总是与您在该可视功能列表中看到的内容相匹配.它还将显示当前启用/禁用的内容.找到要启用的功能(本例中为NetFx3)后,运行以下命令:
dism /online /Enable-Feature /FeatureName:NetFx3
正如理查德所说,你可以通过简单地将“启用”切换为“禁用”来禁用某项功能.
dism /online /disable-Feature /FeatureName:NetFx3
注意:有时需要重新启动才能查看Windows功能的更改.
Python sys.maxint,Linux和Windows上的sys.maxunicode
在64位Debian Linux 6上:
Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48)[GCC 4.4.5] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> sys.maxint9223372036854775807>>> sys.maxunicode1114111
在64位Windows 7上:
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] onwin32Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> sys.maxint2147483647>>> sys.maxunicode65535
两种操作系统都是64位的。根据Wikipedia的介绍,它们具有sys.maxunicode。unicode中有1,114,112个代码点。Windows上的sys.maxunicode错误吗?
为什么它们具有不同的sys.maxint?
答案1
小编典典我不知道您的问题是什么,但 在Windows上sys.maxunicode
没有 错 。
见文档:
sys.maxunicode
一个整数,为Unicode字符提供最大的支持代码点。此值取决于配置选项,该选项指定将Unicode字符存储为UCS-2还是UCS-4。
Windows上的Python使用UCS-2,因此最大代码点为65,535(并且补充平面字符由2 * 16位“代理对”编码)。
关于sys.maxint
,这显示Python
2在什么时候从“简单整数”(123
)切换到“长整数”(12345678987654321L
)。显然,适用于Windows的Python使用32位,而适用于Linux的Python使用64位。从Python
3开始,这已经变得无关紧要,因为简单和长整数类型已合并为一个。因此,sys.maxint
不再是Python 3。
python unicode rendering:如何知道字体中是否缺少unicode字符
当然,一旦我打印出unicode字符,我就可以查看输出,然后确定所选字体是否错过了特定的unicode字符.但是,在我自动打印之前是否有一种方法可以告诉我,而不必依靠我自己的人眼确定字体中是否包含字符?
我还要澄清我知道的字体比其他字体更完整.我的问题不是我可以使用哪种字体,所以如果我称之为“打印”,我通常会有合理的输出.还请忽略我如何打印角色或我是否真的要打印角色的问题.我的问题很简单,对于任何给定的字体,我如何判断字体中是否缺少unicode字符,而不使用依赖于人类对输出的判断的任何手动过程.
解决方法
简而言之,可以安装fonttools包,为其提供任何感兴趣的.ttf字体文件的路径,并检查字体文件的unicode映射表中是否包含长形式的感兴趣的unicode字符.
from fontTools.ttLib import TTFont font = TTFont(fontpath) # specify the path to the font in question def char_in_font(unicode_char,font): for cmap in font['cmap'].tables: if cmap.isUnicode(): if ord(unicode_char) in cmap.cmap: return True return False
然后只需调用char_in_font函数来检查字体中是否包含unicode字符.
Python命令行参数(Windows)
我正在运行32位Windows 7和Python 2.7。
我正在尝试编写可以从CMD运行的命令行Python脚本。我正在尝试为sys.argv
[1]分配一个值。我的脚本的目的是计算文件的MD5哈希值。在命令行中调用脚本时将输入此文件,因此sys.argv [1]应该表示要散列的文件。
这是我的代码如下:
import sysimport hashlibfilename = sys.argv[1]def md5Checksum(filePath): fh = open(filePath, ''rb'') m = hashlib.md5() while True: data = fh.read(8192) if not data: break m.update(data) return m.hexdigest()# print len(sys.argv)print ''The MD5 checksum of text.txt is'', md5Checksum(filename)
每当我运行此脚本时,都会收到错误消息:
filename = sys.argv[1]IndexError: list index out of range
要调用我的脚本,例如,我一直在写“ script.py
test.txt”。脚本和源文件都在同一目录中。我已经测试了len(sys.argv),它只返回一个值,即python脚本名称。
有什么建议?我只能假设这是我通过CMD调用代码的方式
答案1
小编典典尝试使用运行脚本python script.py test.txt
,您可能无法.py
理解扩展程序与扩展的关联。
我们今天的关于在Windows上的Python 2.x中从命令行参数读取Unicode字符和python 读取命令行输入的分享已经告一段落,感谢您的关注,如果您想了解更多关于powershell – 如何在Windows 10中从命令行打开/关闭Windows功能?、Python sys.maxint,Linux和Windows上的sys.maxunicode、python unicode rendering:如何知道字体中是否缺少unicode字符、Python命令行参数(Windows)的相关信息,请在本站查询。
本文标签: