将json文件转换为toml文件

问题描述 投票:0回答:1

我正在尝试使用 python 模块

JSON
Toml
文件转换为
toml
文件,但看起来由于某种未知原因,该模块跳过了一些对我来说很重要的键/值。有谁知道是什么问题吗?

JSON 文件:

{"amqp_url": "amqps://fedora:@rabbitmq.fedoraproject.org/%2Fpublic_pubsub", "callback": "fedora_messaging.example:printer", "tls": {"ca_cert": "/etc/fedora-messaging/cacert.pem", "keyfile": "/etc/fedora-messaging/fedora-key.pem", "certfile": "/etc/fedora-messaging/fedora-cert.pem"}, "client_properties": {"app": "Example Application", "app_url": "https://github.com/fedora-infra/fedora-messaging", "app_contacts_email": ["[email protected]"]}, "exchanges": {"amq.topic": {"type": "topic", "durable": true, "auto_delete": false, "arguments": {}}}, "queues": {"00000000-0000-0000-0000-000000000000": {"durable": false, "auto_delete": true, "exclusive": true, "arguments": {}}}, "bindings": [{"queue": "00000000-0000-0000-0000-000000000000", "exchange": "amq.topic", "routing_keys": ["#"]}], "consumer_config": {"example_key": "for my consumer"}, "qos": {"prefetch_size": 0, "prefetch_count": 25}, "log_config": {"version": 1, "disable_existing_loggers": true, "formatters": {"simple": {"format": "[%(levelname)s %(name)s] %(message)s"}}, "handlers": {"console": {"class": "logging.StreamHandler", "formatter": "simple", "stream": "ext://sys.stdout"}}, "loggers": {"fedora_messaging": {"level": "INFO", "propagate": false, "handlers": ["console"]}, "twisted": {"level": "INFO", "propagate": false, "handlers": ["console"]}, "pika": {"level": "WARNING", "propagate": false, "handlers": ["console"]}}, "root": {"level": "ERROR", "handlers": ["console"]}}}

预期文件:

amqp_url = "amqps://fedora:@rabbitmq.fedoraproject.org/%2Fpublic_pubsub" 
callback = "fedora_messaging.example:printer"

[tls]
ca_cert = "/etc/fedora-messaging/cacert.pem"
keyfile = "/etc/fedora-messaging/fedora-key.pem"
certfile = "/etc/fedora-messaging/fedora-cert.pem"

[client_properties]
app = "Example Application"

app_url = "https://github.com/fedora-infra/fedora-messaging"

app_contacts_email = ["[email protected]"]

[exchanges."amq.topic"]
type = "topic"
durable = true
auto_delete = false
arguments = {}

[queues.00000000-0000-0000-0000-000000000000]
durable = false
auto_delete = true
exclusive = true
arguments = {}

[[bindings]]
queue = "00000000-0000-0000-0000-000000000000"
exchange = "amq.topic"
routing_keys = ["#"]

[consumer_config]
example_key = "for my consumer"

[qos]
prefetch_size = 0
prefetch_count = 25

[log_config]
version = 1
disable_existing_loggers = true

[log_config.formatters.simple]
format = "[%(levelname)s %(name)s] %(message)s"

[log_config.handlers.console]
class = "logging.StreamHandler"
formatter = "simple"
stream = "ext://sys.stdout"

[log_config.loggers.fedora_messaging]
level = "INFO"
propagate = false
handlers = ["console"]

[log_config.loggers.twisted]
level = "INFO"
propagate = false
handlers = ["console"]

[log_config.loggers.pika]
level = "WARNING"
propagate = false
handlers = ["console"]

[log_config.root]
level = "ERROR"
handlers = ["console"]

转载文件:

amqp_url = "amqps://fedora:@rabbitmq.fedoraproject.org/%2Fpublic_pubsub"
callback = "fedora_messaging.example:printer"
[[bindings]]
queue = "00000000-0000-0000-0000-000000000000"
exchange = "amq.topic"
routing_keys = [ "#",]

[tls]
ca_cert = "/etc/fedora-messaging/cacert.pem"
keyfile = "/etc/fedora-messaging/fedora-key.pem"
certfile = "/etc/fedora-messaging/fedora-cert.pem"

[client_properties]
app = "Example Application"
app_url = "https://github.com/fedora-infra/fedora-messaging"
app_contacts_email = [ "[email protected]",]

[consumer_config]
example_key = "for my consumer"

[qos]
prefetch_size = 0
prefetch_count = 25

[log_config]
version = 1
disable_existing_loggers = true

[exchanges."amq.topic"]
type = "topic"
durable = true
auto_delete = false

[queues.00000000-0000-0000-0000-000000000000]
durable = false
auto_delete = true
exclusive = true

[log_config.root]
level = "ERROR"
handlers = [ "console",]

[exchanges."amq.topic".arguments]

[queues.00000000-0000-0000-0000-000000000000.arguments]

[log_config.formatters.simple]
format = "[%(levelname)s %(name)s] %(message)s"

[log_config.handlers.console]
class = "logging.StreamHandler"
formatter = "simple"
stream = "ext://sys.stdout"

[log_config.loggers.fedora_messaging]
level = "INFO"
propagate = false
handlers = [ "console",]

[log_config.loggers.twisted]
level = "INFO"
propagate = false
handlers = [ "console",]

[log_config.loggers.pika]
level = "WARNING"
propagate = false
handlers = [ "console",]

复制的文件会跳过例如大多数空键/值对:

arguments = {}

这是我的代码:

import toml
import json

with open("config.json") as source:
    config = json.loads(source.read())

with open("conf1.toml", "w") as f:
    f.write(toml.dumps(config))
python json config toml
1个回答
0
投票

没有跳过的部分。显然,toml 将所有字典 {} 转换为 toml 表 []。

交易所下的

arguments = {}
已转化为
[exchanges."amq.topic".arguments]

队列下的也一样:

[queues.00000000-0000-0000-0000-000000000000.arguments]

© www.soinside.com 2019 - 2024. All rights reserved.