Python - 解析半 JSON 格式的文本

发布于 2025-01-10 23:45:09 字数 686 浏览 0 评论 0原文

我一直在思考一个问题,但我似乎无法找出正确的前进道路,所以这里是。

我正在尝试解析一个配置文件,但它的编写方式与普通的 .conf 文件不同,并且几乎采用 Half-JSON 格式。下面是如何写出数据的示例:

{
  TEST {
    test1 = "test description"
    test2 = "test description 2"
    test inner {
      innertest = "(something && something)"
      innertest2 = """something else"""
    }
  TEST2 {
    test1 = "test description"
    test2 = "test description 2"
    test inner {
      innertest = "(something && something)"
      innertest2 = """something else"""
    }
}

如您所见,它具有 JSON 的缩进和嵌套,但没有使用普通 JSON 解析器所需的双引号/引号。

有人建议如何将其解析为 python 吗?我的实际最终目标只是使用 Pandas 将特定字段名称(例如 test1、test2)解析到表中,但如果您能想到一种方法可以绕过此问题,请随意提出建议。

I've been mulling over a problem, but I can't seem to figure out the right path forward, so here goes.

I'm attempting to parse a Config file, however it's not written like a normal .conf file, and is almost in a Half-JSON format. Here's an example of how the data is written out:

{
  TEST {
    test1 = "test description"
    test2 = "test description 2"
    test inner {
      innertest = "(something && something)"
      innertest2 = """something else"""
    }
  TEST2 {
    test1 = "test description"
    test2 = "test description 2"
    test inner {
      innertest = "(something && something)"
      innertest2 = """something else"""
    }
}

As you can see, it's got the indentation and nesting of JSON, but without the double quotes/quotes needed to use the normal JSON parser.

Does anyone have a suggestion on how to parse this into python? My actual end goal was just to parse specific field names (e.g. test1, test2) into a table using Pandas, but if there's a way you can think of that bypasses this, feel free to sugest that also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文