我的问题是我想执行类似于httpie的操作。例子
cli foo:='["http","pie"]'
我在代码中所做的就是这个
@click.command()
@click.argument('options', nargs=1)
def dummy(options):
key, value = options.split(':=')
click.echo({key: json.loads(value)})
为了进行测试,我在shell中尝试了以下命令:
$ ibc dummy star:="{"foo":"bar"}"
和
$ ibc dummy star:={"foo":"bar"}
(我有一台Windows 10计算机)
我得到一个json.JSONDecodeError
。有人可以帮助我调试此问题吗?
堆栈跟踪如下:
Traceback (most recent call last):
File "C:\Users\rolla\.virtualenvs\infoblox_client-tZbB1-ed\Scripts\ibc-script.py", line 11, in <module>
load_entry_point('infoblox-client', 'console_scripts', 'ibc')()
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 717, in main
rv = self.invoke(ctx)
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\users\rolla\.virtualenvs\infoblox_client-tzbb1-ed\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "d:\projets\python\infoblox_client\infoblox\scripts\__init__.py", line 31, in dummy
click.echo({key: json.loads(value)})
File "C:\Users\rolla\AppData\Local\Programs\Python\Python37-32\Lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\rolla\AppData\Local\Programs\Python\Python37-32\Lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\rolla\AppData\Local\Programs\Python\Python37-32\Lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
最诚挚的问候。
答案 0 :(得分:0)
这可能是因为您的字符串未正确转义。例如,当您输入"{"foo":"bar"}"
时,Python只会看到一个字符串"{"
,然后是不清楚的字母foo
,然后是另一个字符串":"
,依此类推。通常,在Python中,如果要在字符串中包含引号,则可以将单引号放在双引号中(例如"He said 'hello' to me"
),也可以将双引号放在单引号中(例如'He said "hello" to me'
),或者转义用反斜杠引号(例如"he said \"hello\" to me"
)。在这种情况下,您必须使用last选项,因为它期望两个地方都用双引号引起来:
$ibc dummy star:="{\"foo\":\"bar\"}"