无法在Python中将JSON序列化字典序列化为文件

时间:2016-11-24 07:13:16

标签: python json serialization save

为python道歉,我不能100%确定为什么这会失败,因为我看到的所有示例代码都非常相似。

import io
import json
import argparse
from object_detection.helpers import average_bbox

ap = argparse.ArgumentParser()
ap.add_argument("-o","--output",required=True,help="Output file name.")
ap.add_argument("-c","--class",help="Object class name")
ap.add_argument("-a","--annotations",required=True,help="File path annotations are located in")
args = vars(ap.parse_args())

(avgW,avgH) = average_bbox(args["annotations"])

if args["class"] is None:
    name = args["annotations"].split("/")[-1]
else:
    name = args["class"]

with io.open(args["output"],'w') as f:
    o = {}
    o["class"] = name
    o["avgWidth"] = avgW
    o["avgHeight"] = avgH
    f.write(json.dumps(o,f))

name,avgW和avgH都是有效值。 avgW和avgH是数字,name是字符串。输出似乎是创建文件的有效路径。 我得到的错误是

Traceback (most recent call last):
  File "compute_average_bbox.py", line 19, in <module>
    with io.open(argparse["output"],'w') as f:
TypeError: 'module' object has no attribute '__getitem__'

任何帮助都将不胜感激。

0 个答案:

没有答案