gpt4 book ai didi

python - 如何在 .txt 文件中的 JSON 对象之间添加逗号,然后在 Python 中将其转换为 JSON 数组

转载 作者:太空宇宙 更新时间:2023-11-04 02:01:40 24 4
gpt4 key购买 nike

我正在读取一个 txt 文件,其中包含 JSON 对象,其中对象没有用逗号分隔。我想在 json 对象之间添加逗号并将它们全部放入 JSON 列表或数组中。

我已尝试使用 JSON.loads,但出现 JSON 解码错误。所以我意识到我应该在 .txt 文件中存在的不同对象之间放置逗号

下面是.txt文件内容的例子

{
"@mdate": "2011-01-11",
"@key": "journals/acta/Saxena96",
"author": {
"ftail": "\n",
"ftext": "Sanjeev Saxena"
},
"title": {
"ftail": "\n",
"ftext": "Parallel Integer Sorting and Simulation Amongst CRCW Models."
},
"pages": {
"ftail": "\n",
"ftext": "607-619"
},
"year": {
"ftail": "\n",
"ftext": "1996"
},
"volume": {
"ftail": "\n",
"ftext": "33"
},
"journal": {
"ftail": "\n",
"ftext": "Acta Inf."
},
"number": {
"ftail": "\n",
"ftext": "7"
},
"url": {
"ftail": "\n",
"ftext": "db/journals/acta/acta33.htmlfSaxena96"
},
"ee": {
"ftail": "\n",
"ftext": "http://dx.doi.org/10.1007/BF03036466"
},
"ftail": "\n",
"ftext": "\n"
}{
"@mdate": "2011-01-11",
"@key": "journals/acta/Simon83",
"author": {
"ftail": "\n",
"ftext": "Hans-Ulrich Simon"
},
"title": {
"ftail": "\n",
"ftext": "Pattern Matching in Trees and Nets."
},
"pages": {
"ftail": "\n",
"ftext": "227-248"
},
"year": {
"ftail": "\n",
"ftext": "1983"
},
"volume": {
"ftail": "\n",
"ftext": "20"
},
"journal": {
"ftail": "\n",
"ftext": "Acta Inf."
},
"url": {
"ftail": "\n",
"ftext": "db/journals/acta/acta20.htmlfSimon83"
},
"ee": {
"ftail": "\n",
"ftext": "http://dx.doi.org/10.1007/BF01257084"
},
"ftail": "\n",
"ftext": "\n"
}

''''''''''''''''''''''''''''''''''

预期结果:

''''''''''''''''''''''''''''''''''

[
{
"@mdate": "2011-01-11",
"@key": "journals/acta/Saxena96",
"author": {
"ftail": "\n",
"ftext": "Sanjeev Saxena"
},
"title": {
"ftail": "\n",
"ftext": "Parallel Integer Sorting and Simulation Amongst CRCW Models."
},
"pages": {
"ftail": "\n",
"ftext": "607-619"
},
"year": {
"ftail": "\n",
"ftext": "1996"
},
"volume": {
"ftail": "\n",
"ftext": "33"
},
"journal": {
"ftail": "\n",
"ftext": "Acta Inf."
},
"number": {
"ftail": "\n",
"ftext": "7"
},
"url": {
"ftail": "\n",
"ftext": "db/journals/acta/acta33.htmlfSaxena96"
},
"ee": {
"ftail": "\n",
"ftext": "http://dx.doi.org/10.1007/BF03036466"
},
"ftail": "\n",
"ftext": "\n"
},
{
"@mdate": "2011-01-11",
"@key": "journals/acta/Simon83",
"author": {
"ftail": "\n",
"ftext": "Hans-Ulrich Simon"
},
"title": {
"ftail": "\n",
"ftext": "Pattern Matching in Trees and Nets."
},
"pages": {
"ftail": "\n",
"ftext": "227-248"
},
"year": {
"ftail": "\n",
"ftext": "1983"
},
"volume": {
"ftail": "\n",
"ftext": "20"
},
"journal": {
"ftail": "\n",
"ftext": "Acta Inf."
},
"url": {
"ftail": "\n",
"ftext": "db/journals/acta/acta20.htmlfSimon83"
},
"ee": {
"ftail": "\n",
"ftext": "http://dx.doi.org/10.1007/BF01257084"
},
"ftail": "\n",
"ftext": "\n"
}
]

''''''''''''''''''''

最佳答案

您可以使用 reqexp 在对象之间添加逗号:

import re

with open('name.txt', 'r') as input, open('out.txt', 'w') as output:
output.write("[\n")
for line in input:
line = re.sub('}{', '},{', line)
output.write(' '+line)
output.write("]\n")

关于python - 如何在 .txt 文件中的 JSON 对象之间添加逗号,然后在 Python 中将其转换为 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55555095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com