<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[python 读取和写入yaml配置文件]]></title><description><![CDATA[<p dir="auto">python 读取和写入yaml配置文件</p>
]]></description><link>http://community.bwbot.org/topic/16558/python-读取和写入yaml配置文件</link><generator>RSS for Node</generator><lastBuildDate>Tue, 10 Mar 2026 09:27:51 GMT</lastBuildDate><atom:link href="http://community.bwbot.org/topic/16558.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 03 Mar 2023 05:54:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to python 读取和写入yaml配置文件 on Fri, 03 Mar 2023 11:37:14 GMT]]></title><description><![CDATA[<p dir="auto">说:</p>
<blockquote>
<p dir="auto">继续</p>
</blockquote>
<p dir="auto">好的，接下来我补充一些内容：</p>
<p dir="auto">如果你的yaml文件中有很多注释，你可以使用<code>FullLoader</code>来读取文件。像这样：</p>
<pre><code class="language-python">import yaml

with open('config.yml', 'r') as f:
    config = yaml.load(f, Loader=yaml.FullLoader)

print(config['database'])
</code></pre>
<p dir="auto"><code>yaml.load</code>函数的<code>Loader</code>参数指定了解释器，而<code>yaml.FullLoader</code>解释器支持注释和其他所有的YAML标准功能。更多关于不同解释器的信息可以在PyYAML的文档中找到。</p>
<p dir="auto">如果你的yaml文件比较复杂，你可以使用<code>ruamel.yaml</code>模块，它提供了更多的功能，例如支持多文档和修改文件等。下面是一个读取示例：</p>
<pre><code class="language-python">import ruamel.yaml

with open('config.yml', 'r') as f:
    config = ruamel.yaml.YAML().load(f)

print(config['database'])
</code></pre>
<p dir="auto"><code>ruamel.yaml.YAML</code>实例提供了一些额外的方法，例如<code>.dump</code>方法可以将一个字典写入到YAML文件中。</p>
<p dir="auto">至于写入yaml文件，你需要使用<code>yaml.dump</code>函数。在dump函数中使用默认参数即可，它将优雅地将一个字典转化为yaml文件格式：</p>
<pre><code class="language-python">import yaml

config = {
    'database': {
        'host': 'localhost',
        'port': 3306,
        'username': 'root',
        'password': 'password'
    }
}

with open('config.yml', 'w') as f:
    yaml.dump(config, f)

</code></pre>
<p dir="auto">上述代码将会将字典<code>config</code>写入到<code>config.yml</code>文件中，生成以下内容：</p>
<pre><code class="language-yaml">database:
  host: localhost
  password: password
  port: 3306
  username: root
</code></pre>
<p dir="auto">希望能够帮到你。</p>
]]></description><link>http://community.bwbot.org/post/17030</link><guid isPermaLink="true">http://community.bwbot.org/post/17030</guid><dc:creator><![CDATA[小助理]]></dc:creator><pubDate>Fri, 03 Mar 2023 11:37:14 GMT</pubDate></item><item><title><![CDATA[Reply to python 读取和写入yaml配置文件 on Fri, 03 Mar 2023 11:36:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://community.bwbot.org/uid/1633">@小助理</a> 继续</p>
]]></description><link>http://community.bwbot.org/post/17029</link><guid isPermaLink="true">http://community.bwbot.org/post/17029</guid><dc:creator><![CDATA[weijiz]]></dc:creator><pubDate>Fri, 03 Mar 2023 11:36:51 GMT</pubDate></item><item><title><![CDATA[Reply to python 读取和写入yaml配置文件 on Fri, 03 Mar 2023 05:55:05 GMT]]></title><description><![CDATA[<p dir="auto">首先需要安装PyYAML模块：</p>
<pre><code>pip install PyYAML
</code></pre>
<p dir="auto">然后就可以读取和写入yaml配置文件了。下面是读取示例：</p>
<pre><code class="language-python">import yaml

with open('config.yml', 'r') as f:
    config = yaml.safe_load(f)

print(config['database'])
</code></pre>
<p dir="auto">下面是写入示例：</p>
<pre><code class="language-python">import yaml

config = {
    'database': {
        'host': 'localhost',
        'port': 3306,
        'username': 'root',
        'password': 'password'
    }
}

with open('config.yml', 'w') as f:
    yaml.dump(config, f)
</code></pre>
<p dir="auto">以上代码将会将一个名为<code>config.yml</code>的yaml配置文件读取到一个字典中，并打印出其中名为<code>database</code>的键的值。而写入示例将会将一个包含有数据库连接信息的字典写入到名为<code>config.yml</code>的文件中。</p>
]]></description><link>http://community.bwbot.org/post/17028</link><guid isPermaLink="true">http://community.bwbot.org/post/17028</guid><dc:creator><![CDATA[小助理]]></dc:creator><pubDate>Fri, 03 Mar 2023 05:55:05 GMT</pubDate></item></channel></rss>