【发布时间】:2017-09-17 13:02:27
【问题描述】:
我有一个 Perl (.pm) 文件,我一直在尝试将其转换为 JSON 表示,以便我可以在其中搜索关键字。
我的脚本目录是/Devices/bin/script.py。 .pm 文件所在的目录位于上一层,即/Devices/Devices.PM。 Devices.PM 只是一个静态文件,其中包含结构为字典和列表的数据,如下所示:
mylist = (
blah => {
other =9,
houses => 1,
etc => [
{ etc => '5',
ppl => '3,
things => "etc",
animals => [ 'blah-228', 'blah-229' ]
},
{ etc => '9',
ppl => '5',
things => "22",
animals => [ 'blah-2', 'blah-5' ]
},
],
partner => 'ets',
location => 'wherever',
},
blah => {
ppl => 6,
place => 101,
things => 'etc',
animals => 'whatever',
},
我有:
os.chdir("..")
with open('Devices.pm', 'r') as f:
json_obj = json.dumps(f)
print json_obj
但我不断收到TypeError: <open file 'SwitchConfig.pm', mode 'w' at 0x10c1f69c0> is not JSON serializable。我尝试了几种方法都没有运气,包括json.dump(s)。我不断收到不同的错误。我是否没有正确理解我的 .pm 文件结构以便将其转换为 JSON 表示形式?
【问题讨论】:
-
似乎与Python No JSON object could be decoded这个问题有关,也许可以尝试将文件内容复制到jsonlist.com的验证器中并根据需要进行调整
-
在python 3中,需要读取文件
f.read()