- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我在 Ubuntu 16.04 上运行 python 脚本时,出现以下错误。
当我运行相同的代码但不确定哪个包未正确安装时,它在 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
命令不向 stdout
返回任何结果的情况。
例如,我的订阅中有一个不存在的资源组,例如 non-exist-rg
。如果我将其作为参数 --resource-group
的值传递,下面的脚本将向 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 :第1行第1列(char 0)
,因为json.loads
无法处理空内容。在这里,对于 json.loads
来说,stdout
或 stderr
的 bytes
值的 decode
函数不是必需的。可以接收如下图的bytes
值。
因此要解决此问题,解决方案是检查 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
值。
希望有帮助。
关于ubuntu linux 上的 python : json.解码器.JSONDecodeError:期望值:第2行第6列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56608396/
当我尝试运行以下代码时,出现 JSONDecodeError 错误。我不知道如何解决它。 我正在尝试流式传输推文。对于几条推文来说它工作正常,突然我收到错误并且流停止,请帮助我如何解决这个问题?更新的
下面是我为发布请求执行的代码并收到此错误 JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDec
我正在尝试在 PythonAnywhere 上部署我的应用程序。一切正常,但是当我调用一个函数时,我的应用程序失败了。 异常类型:JSONDecodeError 异常值:期望值:第 1 行第 1 列(
我正在尝试将一列中文地址翻译成英文。我在这里关注了文档:new column with coordinates using geopy pandas。它是第一次工作(大约30分钟前)。突然,当我重新运
我有一个 API 正在向我发送 POST 请求 (JSON) 以进行测试。我对 JSON 做了很多工作,但突然间它停止工作并给我一个 JSONDecodeError。我尝试了各种各样的方法,比如使用
我已经调试了几个小时,为什么我的代码随机中断并出现此错误:JSONDecodeError: Expecting value: line 1 column 1 (char 0) 这是我的代码: whil
我正在尝试调用以下电话: from simplejson import JSONDecodeError 但是我得到了这个错误: from simplejson import JSONDecodeErr
在今天之前它一直在工作。 我不知道为什么它今天不起作用。 import yfinance as yf df = yf.Ticker('MMM').history(start='2021-01-01',
我想读取之前通过一些网络抓取创建的大型 JSON 文件。但是,当我尝试读取该文件时,收到以下错误消息: JSONDecodeError:需要“,”分隔符:第 1364567 行第 2 列(字符 108
当我运行服务器并查看本地主机时,我得到了这个错误: JSONDecodeError at ...Traceback response = get_response(request) 我做错了什么? 我
有人能告诉我为什么这段代码只工作一次而在第二次我得到一个错误我的代码: import json counter_value = 1 data= {} data['test_device']= [] d
我有一个 json 文件,其中包含以下内容: #create test.json and add content dbutils.fs.put("test.json",'{"type": "abc",
这是我第一次使用 json 文件。我有一个 json 文件 { "schema-map" : [ { "src-file": "employee_master.xlsx
我刚刚开始学习 Django,并尝试创建一个 API。它是一个简单的 API,它从表中获取定义并将其作为响应返回。但每当我尝试在请求中发送关键字时,都会收到错误,类型为“JSONDecodeError
我使用 python 3.4.3 设置 virtualenv 并尝试从 json.decoder 导入 JSONDecodeError from json.decoder import JSONDec
我在 Stack Overflow 上进行了全面搜索,但找不到此问题的答案。我正在尝试使用适用于 Python (3.6.2) 的 Google Translate API (googletrans
我正在编写代码以使用“config.json”文件设置在 solr 中上传模型 (train_and_upload_demo_model.py)。但我收到以下错误: json.decoder.JSON
我正在尝试解析来自 youtube api 的 json 响应数据,但我不断收到错误消息。 这是它窒息的片段: data = json.loads("""{ "entry":{ "etag":"W/\
在 Django 中使用 json.loads 时出现 JSONDecodeError。 POST 数据 (b'csrfmiddlewaretoken=ikpUI4vMMbYivTby6FUkXRGN
我正在从 yfinance 抓取股市数据。在过去的几周里,我的代码运行良好。出于某种原因,现在我收到以下错误: json.decoder.JSONDecodeError: Expecting valu
我是一名优秀的程序员,十分优秀!