试图将对象保存为JSON并在python中加载

时间:2015-07-08 15:08:15

标签: python json powershell

我试图从powershell中的对象创建一个JSON文件,我可以保存它,但是当我尝试从python脚本加载它时,我总是收到一个错误:ValueError:期望值:第1行1(char 0)

这是powershell单线:

$devices = Get-WmiObject Win32_PNPEntity | where {$_.ConfigManagerErrorcode -ne 0}  | Select Name, CompatibleID | ConvertTo-Json | Out-File file.json

我相信它是因为powershell保存它的方式,它看起来像这样:

[
    {
        "Name":  "PCI Data Acquisition and Signal Processing Controller",
        "CompatibleID":  [
                             "PCI\\VEN_8086\u0026DEV_A161\u0026REV_31",
                             "PCI\\VEN_8086\u0026DEV_A161",
                             "PCI\\VEN_8086\u0026CC_118000",
                             "PCI\\VEN_8086\u0026CC_1180",
                             "PCI\\VEN_8086",
                             "PCI\\CC_118000",
                             "PCI\\CC_1180"
                         ]
    },
    {
        "Name":  null,
        "CompatibleID":  [
                             "*PNP0CA0"
                         ]
    },
    {
        "Name":  "PCI Data Acquisition and Signal Processing Controller",
        "CompatibleID":  [
                             "PCI\\VEN_8086\u0026DEV_A127\u0026REV_31",
                             "PCI\\VEN_8086\u0026DEV_A127",
                             "PCI\\VEN_8086\u0026CC_118000",
                             "PCI\\VEN_8086\u0026CC_1180",
                             "PCI\\VEN_8086",
                             "PCI\\CC_118000",
                             "PCI\\CC_1180"
                         ]
    },
    {
        "Name":  "Base System Device",
        "CompatibleID":  [
                             "PCI\\VEN_8086\u0026DEV_1911\u0026REV_00",
                             "PCI\\VEN_8086\u0026DEV_1911",
                             "PCI\\VEN_8086\u0026CC_088000",
                             "PCI\\VEN_8086\u0026CC_0880",
                             "PCI\\VEN_8086",
                             "PCI\\CC_088000",
                             "PCI\\CC_0880"
                         ]
    },
    {
        "Name":  "PCI Device",
        "CompatibleID":  [
                             "PCI\\VEN_8086\u0026DEV_A135\u0026REV_31",
                             "PCI\\VEN_8086\u0026DEV_A135",
                             "PCI\\VEN_8086\u0026CC_000000",
                             "PCI\\VEN_8086\u0026CC_0000",
                             "PCI\\VEN_8086",
                             "PCI\\CC_000000",
                             "PCI\\CC_0000"
                         ]
    }
]

这里是python脚本:

import json
with open(r"C:\Users\azeleznx\PycharmProjects\Intel-Python-project\IT_Spider\scripts\file.json", 'r') as file:
    read = json.load(file)
    print(read)

和错误:

Traceback (most recent call last):
  File "C:/Users/Alex/PycharmProjects/Intel-Python-project/test21.py", line 3, in <module>
    read = json.load(file)
  File "C:\Python34\lib\json\__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Python34\lib\json\__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "C:\Python34\lib\json\decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)

现在我知道JSON对象应该以大括号开头和结尾,但我似乎无法使PowerShell以正确的方式保存它。

修改

感谢Robᵩ为我提供了答案,这里的完整脚本与我一样简单

import subprocess
import json

command = "Write-Host (Get-WmiObject Win32_PNPEntity | where {$_.ConfigManagerErrorcode -ne 0}  " \
          "| Select Name, CompatibleID | ConvertTo-Json)"

out = json.loads(subprocess.check_output(["powershell.exe", '-ExecutionPolicy', 'Unrestricted', command]).decode("utf-8"))
print(out)

1 个答案:

答案 0 :(得分:1)

以下是我对不完整信息的猜测:

您已使用utf16编码在powershell中保存文件,但是您使用utf8编码在Python中打开文件。

在Python脚本中打开文件时,请尝试指定正确的编码。你如何做到这取决于你使用的是Python2还是Python3。