python on ubuntu linux : json.decoder.JSONDecodeError: Expecting value: line 2 column 6
python on ubuntu linux : json.decoder.JSONDecodeError: Expecting value: line 2 column 6
我在 运行 python 脚本 Ubuntu 16.04.
上遇到错误
当我 运行 相同的代码但不确定哪个包未正确安装时,它在 Windows 上工作正常。
import subprocess
import json
#one vnet and one subnet in the resourcegroup.
def get_vnet_name(resourcegroup):
get_vnet_command=["az","network","vnet","list","--resource-group",resourcegroup]
get_vnet=subprocess.run(get_vnet_command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
a=get_vnet.stdout.decode('utf-8')
d=json.loads(a)
for item in d:
vname=item["name"]
subnets=item["subnets"]
for i in subnets:
subnetname=i["name"]
return vname,subnetname
def create_vm(vm_resourcegroup,vm_name, vm_image,vm_username, vm_passowrd,vm_vnet,vm_subnet, vm_size):
create_vm_command=["az","vm","create","--resource-group",vm_resourcegroup,"--name",vm_name,"--image",vm_image, "--admin-username", vm_username,"--admin-password",vm_passowrd,"--vnet-name",vm_vnet,"--subnet",vm_subnet,"--size", vm_size]
create_vm=subprocess.run(create_vm_command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
return
if __name__=="__main__":
rscgroup_name="vm-test-group"
avm_name="testvm1"
avm_image="Win2019Datacenter"
avm_username="myuser"
avm_password="mypassword"
avm_size="Standard_D2_V3"
vault_name = "aqrahkeyvault"
certificate_name = "staticwebsite"
avm_vnet,avm_subnet=get_vnet_name(rscgroup_name)
create_vm(rscgroup_name,avm_name,avm_image,avm_username,avm_password,avm_vnet,avm_subnet,avm_size)
下面是我遇到的与 json.decoder 相关的错误:
root@linuxvm:/home/azureuser# python3.6 test2.py
Traceback (most recent call last):
File "test2.py", line 32, in <module>
avm_vnet,avm_subnet=get_vnet_name(rscgroup_name)
File "test2.py", line 9, in get_vnet_name
d=json.loads(a)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 6 (char 6)
我已尝试安装 python,但未能解决问题。
我尝试重现了您的问题,并成功找到了原因。实际上,您的脚本基本上是正确的,但是您可能不会考虑 az
命令不是 return 任何结果到 stdout
.
的情况
比如我的订阅里有non-existing个资源组,比如non-exist-rg
。如果我将它作为参数--resource-group
的值传递,下面的脚本会将return错误信息传递给stderr
,stdout
值为b''
.
import subprocess
resourcegroup = 'non-exist-rg'
get_vnet_command=["az","network","vnet","list","--resource-group",resourcegroup]
get_vnet=subprocess.run(get_vnet_command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
stdout
& stderr
的结果如下。
>>> get_vnet.stdout
b''
>>> get_vnet.stderr
b"ERROR: Resource group 'non-exist-rg' could not be found.\r\n"
因此,如果您将 stdout
值传递给 json.loads
函数,它会抛出与您的 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
相同的问题,因为 json.loads
无法处理空内容.在这里,对于 stdout
或 stderr
的 bytes
值的 decode
函数对于可以接收 bytes
值的 json.loads
不是必需的下图
所以要修复它,解决方法是检查 stdout
值是否为空或 stderr
值是否不为空。
def get_vnet_name(resourcegroup):
get_vnet_command=["az","network","vnet","list","--resource-group",resourcegroup]
get_vnet=subprocess.run(get_vnet_command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
# decode for stdout is not necessary
# a=get_vnet.stdout.decode('utf-8')
vname,subnetname = '', ''
if get_vnet.stdout == b'':
d=json.loads(get_vnet.stdout)
for item in d:
vname=item["name"]
subnets=item["subnets"]
for i in subnets:
subnetname=i["name"]
return vname,subnetname
然后,您需要在调用 create_vm
方法之前检查 vname
和 subnetname
值。
希望对您有所帮助。
我在 运行 python 脚本 Ubuntu 16.04.
上遇到错误当我 运行 相同的代码但不确定哪个包未正确安装时,它在 Windows 上工作正常。
import subprocess
import json
#one vnet and one subnet in the resourcegroup.
def get_vnet_name(resourcegroup):
get_vnet_command=["az","network","vnet","list","--resource-group",resourcegroup]
get_vnet=subprocess.run(get_vnet_command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
a=get_vnet.stdout.decode('utf-8')
d=json.loads(a)
for item in d:
vname=item["name"]
subnets=item["subnets"]
for i in subnets:
subnetname=i["name"]
return vname,subnetname
def create_vm(vm_resourcegroup,vm_name, vm_image,vm_username, vm_passowrd,vm_vnet,vm_subnet, vm_size):
create_vm_command=["az","vm","create","--resource-group",vm_resourcegroup,"--name",vm_name,"--image",vm_image, "--admin-username", vm_username,"--admin-password",vm_passowrd,"--vnet-name",vm_vnet,"--subnet",vm_subnet,"--size", vm_size]
create_vm=subprocess.run(create_vm_command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
return
if __name__=="__main__":
rscgroup_name="vm-test-group"
avm_name="testvm1"
avm_image="Win2019Datacenter"
avm_username="myuser"
avm_password="mypassword"
avm_size="Standard_D2_V3"
vault_name = "aqrahkeyvault"
certificate_name = "staticwebsite"
avm_vnet,avm_subnet=get_vnet_name(rscgroup_name)
create_vm(rscgroup_name,avm_name,avm_image,avm_username,avm_password,avm_vnet,avm_subnet,avm_size)
下面是我遇到的与 json.decoder 相关的错误:
root@linuxvm:/home/azureuser# python3.6 test2.py
Traceback (most recent call last):
File "test2.py", line 32, in <module>
avm_vnet,avm_subnet=get_vnet_name(rscgroup_name)
File "test2.py", line 9, in get_vnet_name
d=json.loads(a)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 6 (char 6)
我已尝试安装 python,但未能解决问题。
我尝试重现了您的问题,并成功找到了原因。实际上,您的脚本基本上是正确的,但是您可能不会考虑 az
命令不是 return 任何结果到 stdout
.
比如我的订阅里有non-existing个资源组,比如non-exist-rg
。如果我将它作为参数--resource-group
的值传递,下面的脚本会将return错误信息传递给stderr
,stdout
值为b''
.
import subprocess
resourcegroup = 'non-exist-rg'
get_vnet_command=["az","network","vnet","list","--resource-group",resourcegroup]
get_vnet=subprocess.run(get_vnet_command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
stdout
& stderr
的结果如下。
>>> get_vnet.stdout
b''
>>> get_vnet.stderr
b"ERROR: Resource group 'non-exist-rg' could not be found.\r\n"
因此,如果您将 stdout
值传递给 json.loads
函数,它会抛出与您的 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
相同的问题,因为 json.loads
无法处理空内容.在这里,对于 stdout
或 stderr
的 bytes
值的 decode
函数对于可以接收 bytes
值的 json.loads
不是必需的下图
所以要修复它,解决方法是检查 stdout
值是否为空或 stderr
值是否不为空。
def get_vnet_name(resourcegroup):
get_vnet_command=["az","network","vnet","list","--resource-group",resourcegroup]
get_vnet=subprocess.run(get_vnet_command, shell = True, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
# decode for stdout is not necessary
# a=get_vnet.stdout.decode('utf-8')
vname,subnetname = '', ''
if get_vnet.stdout == b'':
d=json.loads(get_vnet.stdout)
for item in d:
vname=item["name"]
subnets=item["subnets"]
for i in subnets:
subnetname=i["name"]
return vname,subnetname
然后,您需要在调用 create_vm
方法之前检查 vname
和 subnetname
值。
希望对您有所帮助。