Python的json.dump()输出似乎不相关

huangapple go评论67阅读模式
英文:

Python's json.dump() output seems not relevant

问题

我希望你一切都好。最近,我决定稍微玩一下JSON文件,但遇到了问题。
我不确定是否犯了某种重大错误,但每当我尝试将字典/字符串变量转换为.json文件时,输出看起来并不像我习惯的那样。
以下是代码:

dictio = """{
    "Forms": [
        {
            "Forms_2D": [
                {
                    "Line": ["test", 0],
                    "Square": ["test", 0],
                    "Triangle": ["test", 0],
                    "Circle": ["test", 0],
                    "Rectangle": ["test", 0],
                    "Parallelogram": ["test", 0],
                    "Rhombus": ["test", 0],
                    "Trapezoid": ["test", 0],
                    "Hexagon": ["test", 0],
                    "Octagon": ["test", 0]
                }
            ],
            "Forms_3D": [
                {
                    "Cube": ["test", 0],
                    "Pyramid": ["test", 0],
                    "Cone": ["test", 0],
                    "Cylinder": ["test", 0],
                    "Sphere": ["test", 0],
                    "Ball": ["test", 0],
                    "Cuboid": ["test", 0]
                }
            ]
        }
    ],
    "Calculations": [
        {
            "Irrational": ["test", 0],
            "Not_a_Number": ["test", 0]
        }
    ]
}"""

with open("info.json", "w") as file:
    json.dump(dictio, file, indent=4, separators=(",", ":"))
file.close()

上面代码的输出如下:

{
    "Forms": [
        {
            "Forms_2D": [
                {
                    "Line": ["test", 0],
                    "Square": ["test", 0],
                    "Triangle": ["test", 0],
                    "Circle": ["test", 0],
                    "Rectangle": ["test", 0],
                    "Parallelogram": ["test", 0],
                    "Rhombus": ["test", 0],
                    "Trapezoid": ["test", 0],
                    "Hexagon": ["test", 0],
                    "Octagon": ["test", 0]
                }
            ],
            "Forms_3D": [
                {
                    "Cube": ["test", 0],
                    "Pyramid": ["test", 0],
                    "Cone": ["test", 0],
                    "Cylinder": ["test", 0],
                    "Sphere": ["test", 0],
                    "Ball": ["test", 0],
                    "Cuboid": ["test", 0]
                }
            ]
        }
    ],
    "Calculations": [
        {
            "Irrational": ["test", 0],
            "Not_a_Number": ["test", 0]
        }
    ]
}

我认为问题与输入文件有关,所以我尝试了很多版本,其中包括制表符或"\n",但都没有成功 - 输出仅在一行中的键和数组之间添加了额外的"\t"和"\n"值。
另一方面,我尝试使用json.loads然后将其添加到json.dump中,如下所示:

x = json.loads(dictio)

with open("info.json", "w") as file:
    json.dump(x, file, indent=4, separators=(",", ":"))
file.close()

但是它再次失败,提示我有一个与','分隔符有关的问题:
json.decoder.JSONDecodeError: 在第1行第42列(字符41)处期望','分隔符

请帮助我解决这个问题:-)

最好的问候,
stackoverflow用户

英文:

I hope you're doing fine.
Recently I decided to tinker a little with JSON files, but was struck by problem.
I am not sure if I make some kind of a cardinal mistake, but whenever I try to convert
a dictionary/string variable to .json file, the output does not really look similar to
what I'm used to.
Here's the code:

dictio = """{"Forms": [{"Forms_2D": [{"Line": ["test": 0 ],"Square": ["test": 0 ],"Triangle": ["test": 0 ],"Circle": ["test": 0],"Rectangle": ["test": 0],"Parallelogram": ["test": 0],"Rhombus": ["test": 0],"Trapezoid": ["test": 0],"Hexagon": ["test": 0],"Octagon": ["test": 0],}],"Forms_3D": [{"Cube": ["test": 0],"Pyramid": ["test": 0],"Cone": ["test": 0],"Cylinder": ["test": 0],"Sphere": ["test": 0],"Ball": ["test": 0],"Cuboid": ["test": 0]}]}],"Calculations": [{"Irrational": ["test": 0],"Not_a_Number": ["test": 0]}]}"""
    
with open("info.json", "w") as file:
     json.dump(dictio,file,indent=4,separators=(',', ': '))
file.close()

The Output of the code above seems like this:

"{\"Forms\": [{\"Forms_2D\": [{\"Line\": [\"test\": 0 ],\"Square\": [\"test\": 0 ],\"Triangle\": [\"test\": 0 ],\"Circle\": [\"test\": 0],\"Rectangle\": [\"test\": 0],\"Parallelogram\": [\"test\": 0],\"Rhombus\": [\"test\": 0],\"Trapezoid\": [\"test\": 0],\"Hexagon\": [\"test\": 0],\"Octagon\": [\"test\": 0],}],\"Forms_3D\": [{\"Cube\": [\"test\": 0],\"Pyramid\": [\"test\": 0],\"Cone\": [\"test\": 0],\"Cylinder\": [\"test\": 0],\"Sphere\": [\"test\": 0],\"Ball\": [\"test\": 0],\"Cuboid\": [\"test\": 0]}]}],\"Calculations\": [{\"Irrational\": [\"test\": 0],\"Not_a_Number\": [\"test\": 0]}]}"

I thought the problem was related to the input file, so I made many
versions with either tabulations or "\n", but with no luck - the output was only altered
with additional "\t" and "\n" values between the keys and arrays within one line.
On another hand I tried to use json.loads and than add it to json.dump like this:

x = json.loads(dictio)

with open("info.json", "w") as file:
     json.dump(x,file,indent=4,separators=(',', ': '))
file.close()

But than again it fails instructing me that there is a problem with ',' delimiter:
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 42 (char 41)

Please help me out Python的json.dump()输出似乎不相关

Best Regards,
stackoverflow-User

答案1

得分: 1

json.dump接受一个真正的字典作为参数。你正在传递一个JSON字符串。

英文:

json.dump takes a real dictionary as an argument. You are passing a JSON string.

huangapple
  • 本文由 发表于 2023年6月22日 06:58:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定