生成 json 转储,字典以逗号分隔
Generate json dump with dictionaries separated in commas
我正在从 python 生成一个 json 文件,但是,我想在每个循环后通过逗号分隔字典,这是代码的一部分:
listA = [computer1,computer2]
listB = [computertype1,computertype2]
for computer, item in zip(listA,listB):
mydict = {
"Computertype": "somevalue",
"computer": [
computer
],
"targert": {
"item": item
}
},
期望的输出应该是:
[
{
"Computertype": "somevalue",
"computer": [
computer1
],
"targert": {
"item": computertype1
}
},
{
"Computertype": "somevalue",
"computer": [
computer2
],
"targert": {
"item": computertype2
}
}
]
所以基本上是第一个循环的第一个结束大括号之后的逗号:},并且都在一个列表中,一个括号在顶部,一个右括号在底部。
当运行代码时,它不会在循环中的每个大括号后显示逗号,它会自动在列表中输入每个循环,有什么建议吗?
我得到的是:
[ {
"Computertype": "somevalue",
"computer": [
computer1
],
"targert": {
"item": computertype1
}
}
]
[ {
"Computertype": "somevalue",
"computer": [
computer2
],
"targert": {
"item": computertype2
}
}]
我想这就是你要找的:
listA = [computer1,computer2]
listB = [computertype1,computertype2]
mylist = []
for computer, item in zip(listA,listB):
mydict = {
"Computertype": "somevalue",
"computer": [
computer
],
"target": {
"item": item
}
}
mylist.append(mydict)
print(mylist)
我正在从 python 生成一个 json 文件,但是,我想在每个循环后通过逗号分隔字典,这是代码的一部分:
listA = [computer1,computer2]
listB = [computertype1,computertype2]
for computer, item in zip(listA,listB):
mydict = {
"Computertype": "somevalue",
"computer": [
computer
],
"targert": {
"item": item
}
},
期望的输出应该是:
[
{
"Computertype": "somevalue",
"computer": [
computer1
],
"targert": {
"item": computertype1
}
},
{
"Computertype": "somevalue",
"computer": [
computer2
],
"targert": {
"item": computertype2
}
}
]
所以基本上是第一个循环的第一个结束大括号之后的逗号:},并且都在一个列表中,一个括号在顶部,一个右括号在底部。
当运行代码时,它不会在循环中的每个大括号后显示逗号,它会自动在列表中输入每个循环,有什么建议吗?
我得到的是:
[ {
"Computertype": "somevalue",
"computer": [
computer1
],
"targert": {
"item": computertype1
}
}
]
[ {
"Computertype": "somevalue",
"computer": [
computer2
],
"targert": {
"item": computertype2
}
}]
我想这就是你要找的:
listA = [computer1,computer2]
listB = [computertype1,computertype2]
mylist = []
for computer, item in zip(listA,listB):
mydict = {
"Computertype": "somevalue",
"computer": [
computer
],
"target": {
"item": item
}
}
mylist.append(mydict)
print(mylist)