我无法使用python将数组附加到多维JSON数组,同时又为其提供了正确的所需格式。
我目前正在从文本文件中读取行,并将其附加到python数组中:
with open("./strings/strings.txt", "r") as stringsFile:
for line in stringsFile:
strings_array.append(line.strip())
strings.txt:
someotherstring
someotherstring2
然后,我打开一个JS文件,获取现有的多维数组并将其解析为JSON,以便可以将我的strings_array附加到它:
with open("./test/strings.js", "r") as stringsJS:
js = stringsJS.read()
js = js.strip("module.exports = ")
test_json = json.loads(js)
test_json.insert(0, [strings_array[0], [strings_array[1]])
print(json.dumps(test_json, indent=4, sort_keys=True))
strings.js:
module.exports = [
["somestring", "somestring"]
]
当我打印json.dumps
时,输出如下:
[
[
"someotherstring",
"someotherstring2"
],
[
"somestring",
"somestring"
]
]
但是所需的输出是:
[
["somestring", "somestring"],
["someotherstring", "someotherstring2"]
]
反正我能达到期望的输出吗?