以下是我的输入file.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<project name="so_project" id="Project-9999">
    <schema name="database1">
        <table name="table1">
            <column name="foo" type="int"/>
            <column name="bar" type="string"/>
            <column name="details_resolution" type="array[object]">
                <column name="timestamp" type="timestamp"/>
                <column name="user_id" type="string"/>
                <column name="user_name" type="string"/>
            </column>
            <column name="details_closure" type="array[object]">
                <column name="timestamp" type="timestamp"/>
                <column name="auto_closure" type="bool"/>
            </column>
        </table>
    </schema>
    <schema name="database2">
        <table name="table1">
            <column name="foo" type="int"/>
            <column name="bar" type="string"/>
            <column name="details" type="array[object]">
                <column name="timestamp" type="timestamp"/>
                <column name="value" type="float"/>
            </column>
        </table>
    </schema>
</project>

..我正试着让这句classic 的嵌套格言:

{
    "database1": {
        "table1": {
            "foo": "int",
            "bar": "string",
            "details_resolution": {
                "timestamp": "timestamp",
                "user_id": "string",
                "user_name": "string"
            },
            "details_closure": {
                "timestamp": "timestamp",
                "auto_closure": "bool"
            }
        }
    },
    "database2": {
        "table1": {
            "foo": "int",
            "bar": "string",
            "details": {
                "timestamp": "timestamp",
                "value": "float"
            }
        }
    }
}

PS : Each database can eventually have more than one table.

I tried some AI codes but none of them gave me the expected result..
I'm sorry guys to not being able to show my attempts !

因此,任何帮助都将不胜感激.

推荐答案

Solution using :

from bs4 import BeautifulSoup

with open("your_file.xml", "r") as f_in:
    soup = BeautifulSoup(f_in.read(), "xml")


def parse_columns(t):
    out = {}
    for c in t.find_all("column", recursive=False):
        if c.find("column"):
            out[c["name"]] = parse_columns(c)
        else:
            out[c["name"]] = c["type"]
    return out


def parse_schema(sch):
    out = {}
    for t in sch.select("table"):
        out[t["name"]] = parse_columns(t)
    return out


out = {}
for sch in soup.select("schema"):
    out[sch["name"]] = parse_schema(sch)

print(out)

打印:

{
    "database1": {
        "table1": {
            "foo": "int",
            "bar": "string",
            "details_resolution": {
                "timestamp": "timestamp",
                "user_id": "string",
                "user_name": "string",
            },
            "details_closure": {"timestamp": "timestamp", "auto_closure": "bool"},
        }
    },
    "database2": {
        "table1": {
            "foo": "int",
            "bar": "string",
            "details": {"timestamp": "timestamp", "value": "float"},
        }
    },
}

Python相关问答推荐

双情节在单个图上切换-pPython

如何分割我的收件箱,以便连续的数字各自位于自己的收件箱中?

如何以实现以下所述的预期行为的方式添加两只Pandas pyramme

如何使用scikit-learn Python库中的Agglomerative集群算法以及集群中声明的对象数量?

Twilio:CallInstance对象没有来自_的属性'

覆盖Django rest响应,仅返回PK

无法使用equals_html从网址获取全文

线性模型PanelOLS和statmodels OLS之间的区别

Pandas 滚动最接近的价值

try 在树叶 map 上应用覆盖磁贴

非常奇怪:tzLocal.get_Localzone()基于python3别名的不同输出?

. str.替换pandas.series的方法未按预期工作

如何从.cgi网站刮一张表到rame?

无法定位元素错误404

Pandas—在数据透视表中占总数的百分比

pandas在第1列的id,第2列的标题,第3列的值,第3列的值?

需要帮助重新调整python fill_between与数据点

如何使用两个关键函数来排序一个多索引框架?

剪切间隔以添加特定日期

在matplotlib中使用不同大小的标记顶部添加批注