Python选项获取空值

Python选项获取空值,python,Python,我尝试使用opts,但无法在其他PC上使用,因为它总是空的。下面是我的代码 import getopt import sys try: print getopt.getopt(sys.argv[1:], "f::c::") opts, args = getopt.getopt(sys.argv[1:], "f::c::") except getopt.GetoptError, err: # print help information and exit: pri

我尝试使用opts,但无法在其他PC上使用,因为它总是空的。下面是我的代码

import getopt
import sys

try:
    print getopt.getopt(sys.argv[1:], "f::c::")
    opts, args = getopt.getopt(sys.argv[1:], "f::c::")
except getopt.GetoptError, err:
    # print help information and exit:
    print str(err) # will print something like "option -a not recognized"
    sys.exit(2)

print opts
print args

funcion = None
tipo = None

for opt, arg in opts:
    if opt in ('-f'):
        funcion = arg
    if opt in ('-c'):
        tipo = arg

print funcion
print tipo
使用测试:

python test.py –f import_dbs –c 1
([('-f', 'imports'), ('-c', '1')], [])
[('-f', 'imports'), ('-c', '1')]
[]
imports
1
([], ['\x96f', 'import_dbs', '\x96c', '1'])
[]
['\x96f', 'import_dbs', '\x96c', '1']
None
None
PC A结果:

python test.py –f import_dbs –c 1
([('-f', 'imports'), ('-c', '1')], [])
[('-f', 'imports'), ('-c', '1')]
[]
imports
1
([], ['\x96f', 'import_dbs', '\x96c', '1'])
[]
['\x96f', 'import_dbs', '\x96c', '1']
None
None
PC B结果:

python test.py –f import_dbs –c 1
([('-f', 'imports'), ('-c', '1')], [])
[('-f', 'imports'), ('-c', '1')]
[]
imports
1
([], ['\x96f', 'import_dbs', '\x96c', '1'])
[]
['\x96f', 'import_dbs', '\x96c', '1']
None
None

问题在于cli命令,而不是代码。您使用破折号(或某种unicode)而不是连字符

$ python test.py –f import_dbs –c 1
None
None
$ python test.py -f import_dbs –c 1
import_dbs
None
$ python test.py -f import_dbs -c 1
import_dbs
1
$ echo "python test.py –f import_dbs –c 1" | od -c
0000000    p   y   t   h   o   n       t   e   s   t   .   p   y       –
0000020   **  **   f       i   m   p   o   r   t   _   d   b   s       –
0000040   **  **   c       1  \n                                        
0000046
$ echo "python test.py -f import_dbs -c 1" | od -c
0000000    p   y   t   h   o   n       t   e   s   t   .   p   y       -
0000020    f       i   m   p   o   r   t   _   d   b   s       -   c    
0000040    1  \n                                                        
0000042

可能是由于剪切和粘贴或奇怪的键盘映射造成的。

什么是“PC A”和“PC B”?PC B显示的是unicode破折号,而不是PC A的简单连字符!我使用的是ConEmu控制台,我用windows cmd进行测试,它可以正常工作!好。如果有帮助,请随意标记我的答案为正确。