json 如何删除文本文件末尾的2个字母[关闭]

n8ghc7c1  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(110)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

5天前关闭。
这篇文章5天前被编辑并提交审查。
Improve this question
我的JSON文件看起来像这样:

{
    "key": "test", "expiry": "test",
    "key": "eb467ff5-da95-47b5-b2d4-d5d6e9141a47", "expiry": "2022-12-13",
    "key": "17fd8fd1-b920-4e42-8e81-450757f9c79a", "expiry": "2022-12-13",
    "key": "91a969af-263f-46c1-a2f3-12e876403de6", "expiry": "2022-12-13",
}

我希望它删除最后一个“”,这样JSON就不会有任何错误。我该怎么做呢?错误是最后一个,在文件中:https://prnt.sc/mNVxXWOVBAOS
我该怎么做?

eyh26e7m

eyh26e7m1#

s = """
{
    "key": "test", "expiry": "test",
    "key": "eb467ff5-da95-47b5-b2d4-d5d6e9141a47", "expiry": "2022-12-13",
    "key": "17fd8fd1-b920-4e42-8e81-450757f9c79a", "expiry": "2022-12-13",
    "key": "91a969af-263f-46c1-a2f3-12e876403de6", "expiry": "2022-12-13",
}
"""

comma_index = s.rfind(',')
s_cleaned = s[:comma_index] + s[comma_index+1:]
print(s)

输出量:

|| [python -u "hi2.py"]
|| 
|| {
||     "key": "test", "expiry": "test",
||     "key": "eb467ff5-da95-47b5-b2d4-d5d6e9141a47", "expiry": "2022-12-13",
||     "key": "17fd8fd1-b920-4e42-8e81-450757f9c79a", "expiry": "2022-12-13",
||     "key": "91a969af-263f-46c1-a2f3-12e876403de6", "expiry": "2022-12-13"
|| }
|| 
|| [Finished in 0 seconds]

相关问题