擅长:python、mysql、java
<p>{a1}库可以为您执行此操作。还有一些类似<code>json</code>、<code>pyyaml</code>等的方法也差不多。在您的示例中,首先需要将信息以以下格式存储在字典中:</p>
<pre class="lang-py prettyprint-override"><code>data = {
"target": {
"ip": "xx.xx.xx.xx",
"os": {
"os": "win 10",
"Arch": "x64"
},
"ports": {
"ports": ["1", "2"],
"1": {
"service": "xxx",
"ver": "5.9",
}
}
}
}
</code></pre>
<p>然后,您可以执行以下操作:</p>
<pre class="lang-py prettyprint-override"><code>import toml
toml_string = toml.dumps(data) # Output to a string
output_file_name = "output.toml"
with open(output_file_name, "w") as toml_file:
toml.dump(data, toml_file)
</code></pre>
<p>类似地,您也可以使用以下方法将</strong>toml文件加载到字典格式中:</p>
<pre class="lang-py prettyprint-override"><code>import toml
toml_dict = toml.loads(toml_string) # Read from a string
input_file_name = "input.toml"
with open(input_file_name) as toml_file:
toml_dict = toml.load(toml_file)
</code></pre>
<hr/>
<p>如果您不想使用<code>toml</code>而想使用<code>yaml</code>或<code>json</code>,那么只需在所有命令中用<code>yaml</code>或<code>json</code>替换<code>toml</code>即可。他们都使用相同的呼叫约定</p>