数据处理与 Python
<span style="color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;">Python 是处理数据的理想语言。它支持 JSON 和 XML 开箱即用,但 PyPI 上也有许多外部库来帮助您处理数据,如 PyYAML。在这一章中,我们将深入探讨各种数据的处理。</span><p></p><div><h2 id="h-importing-the-json-library-in-python" style="box-sizing: inherit; margin-top: 40px; margin-bottom: 5px; border: 0px; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 35px; font-weight: 400; line-height: 1.2em; color: rgb(34, 34, 34);"><font _mstmutation="1" _msthash="1257555" _msttexthash="21305024" style="box-sizing: inherit;">导入Python中的JSON库</font><span class="ez-toc-section-end" style="box-sizing: inherit;"></span></h2><p _msthash="675508" _msttexthash="306372235" style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;">Python 船与一个强大和优雅<font color="#1e73be"><span style="box-sizing: inherit; transition: color 0.1s ease-in-out 0s, background-color 0.1s ease-in-out 0s;">的 JSON 库</span></font>,以帮助您解码和编码 JSON。它可以导入:</p><pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="" style="box-sizing: inherit; margin-bottom: 1.5em; padding: 20px; border: 0px; background: rgba(0, 0, 0, 0.05); font-family: inherit; font-size: 17px; line-height: normal; overflow: auto; max-width: 100%; color: rgb(34, 34, 34);">import json</pre><p _msthash="675509" _msttexthash="191358063" style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;">这个库是Python的一部分,所以你不需要安装它与<font color="#1e73be"><span style="box-sizing: inherit; transition: color 0.1s ease-in-out 0s, background-color 0.1s ease-in-out 0s;">Pip包经理</span></font>。</p><h2 id="h-how-to-parse-json-in-python" style="box-sizing: inherit; margin-top: 40px; margin-bottom: 5px; border: 0px; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 35px; font-weight: 400; line-height: 1.2em; color: rgb(34, 34, 34);"><span class="ez-toc-section" id="How_to_parse_JSON_in_Python" style="box-sizing: inherit;"></span><font _mstmutation="1" _msthash="780299" _msttexthash="23519600" style="box-sizing: inherit;">如何解析 Python 中的 Json</font><span class="ez-toc-section-end" style="box-sizing: inherit;"></span></h2><p style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;"><font _mstmutation="1" _msthash="675510" _msttexthash="368223401" style="box-sizing: inherit;">解析一串 JSON 数据(也称为解码 JSON)就像使用一样简单。负载是负载字符串的缩头。</font><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">json.loads(…)</code></p><div class="code-block code-block-5" style="box-sizing: inherit; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px; margin-top: 8px; margin-bottom: 8px; clear: both;"><ins class="adsbygoogle" data-ad-client="ca-pub-2054560618904023" data-ad-slot="2661975099" data-ad-format="auto" data-full-width-responsive="true" style="box-sizing: inherit; text-decoration-line: none; display: block; min-height: 200px;"></ins></div><p _msthash="675511" _msttexthash="17416815" style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;">它转换:</p><ul style="box-sizing: inherit; margin-bottom: 1.5em; margin-left: 3em; border: 0px; list-style-position: initial; list-style-image: initial; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;"><li _msthash="934544" _msttexthash="11723452" style="box-sizing: inherit; margin-left: 0px; border: 0px;"><font color="#1e73be"><span style="box-sizing: inherit; transition: color 0.1s ease-in-out 0s, background-color 0.1s ease-in-out 0s;">字典对象</span></font></li><li _msthash="934545" _msttexthash="22019894" style="box-sizing: inherit; margin-left: 0px; border: 0px;">阵列列表,</li><li _msthash="934546" _msttexthash="315622203" style="box-sizing: inherit; margin-left: 0px; border: 0px;"><font color="#1e73be"><span style="box-sizing: inherit; transition: color 0.1s ease-in-out 0s, background-color 0.1s ease-in-out 0s;">布尔斯</span></font>,<font color="#1e73be"><span style="box-sizing: inherit; transition: color 0.1s ease-in-out 0s, background-color 0.1s ease-in-out 0s;">整数</span></font>, 浮动, 和<font color="#1e73be"><span style="box-sizing: inherit; transition: color 0.1s ease-in-out 0s, background-color 0.1s ease-in-out 0s;">字符串</span></font>被识别为他们是什么, 并会转换成正确的类型在 Python</li><li style="box-sizing: inherit; margin-left: 0px; border: 0px;"><font _mstmutation="1" _msthash="934547" _msttexthash="47896836" style="box-sizing: inherit;">任何将转换为蟒蛇的类型</font><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">null</code><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">None</code></li></ul><p style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;"><font _mstmutation="1" _msthash="675512" _msttexthash="63872900" style="box-sizing: inherit;">下面是一个在操作中的示例:</font><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">json.loads</code></p><pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="" style="box-sizing: inherit; margin-bottom: 1.5em; padding: 20px; border: 0px; background: rgba(0, 0, 0, 0.05); font-family: inherit; font-size: 17px; line-height: normal; overflow: auto; max-width: 100%; color: rgb(34, 34, 34);">>>> import json>>> jsonstring = '{"name": "erik", "age": 38, "married": true}'
>>> data = json.loads(jsonstring)
>>> print(data)
{'name': 'erik', 'age': 38, 'married': True}</pre><p _msthash="675513" _msttexthash="663490711" style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;">输出可能看起来像一个字符串,但它实际上是一个字典,你可以在你的代码中使用,在我们的页面上解释的<font color="#1e73be"><span style="box-sizing: inherit; transition: color 0.1s ease-in-out 0s, background-color 0.1s ease-in-out 0s;">Python字典</span></font>。例如:</p><pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="" style="box-sizing: inherit; margin-bottom: 1.5em; padding: 20px; border: 0px; background: rgba(0, 0, 0, 0.05); font-family: inherit; font-size: 17px; line-height: normal; overflow: auto; max-width: 100%; color: rgb(34, 34, 34);">>>> type(data)
<class 'dict'>
>>> print('Hello', data['name'], "you're", data['age'], 'years old')
Hello erik you're 38 years old</pre><h2 id="h-encoding-json-with-python" style="box-sizing: inherit; margin-top: 40px; margin-bottom: 5px; border: 0px; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 35px; font-weight: 400; line-height: 1.2em; color: rgb(34, 34, 34);"><span class="ez-toc-section" id="Encoding_JSON_with_Python" style="box-sizing: inherit;"></span><font _mstmutation="1" _msthash="709423" _msttexthash="26858351" style="box-sizing: inherit;">编码杰森与蟒蛇</font><span class="ez-toc-section-end" style="box-sizing: inherit;"></span></h2><p style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;"><font _mstmutation="1" _msthash="675514" _msttexthash="881976992" style="box-sizing: inherit;">用 Python 编码 JSON 数据和解码一样简单。使用("转储到字符串"的简称为)将由字典、列表和其他原生类型组成的 Python 对象转换为字符串:</font><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">json.dumps(…)</code></p><pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="" style="box-sizing: inherit; margin-bottom: 1.5em; padding: 20px; border: 0px; background: rgba(0, 0, 0, 0.05); font-family: inherit; font-size: 17px; line-height: normal; overflow: auto; max-width: 100%; color: rgb(34, 34, 34);">>>> data = {'name': 'erik', 'age': 38, 'married': True}
>>> json.dumps(data)
'{"name": "erik", "age": 38, "married": true}'</pre><p _msthash="675515" _msttexthash="851083896" style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;">这是相同的文档,转换回字符串!如果您想让您的 JSON 文档更适合人类阅读,请使用凹痕选项。它将很好地格式化JSON,使用空间字符:</p><pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="" style="box-sizing: inherit; margin-bottom: 1.5em; padding: 20px; border: 0px; background: rgba(0, 0, 0, 0.05); font-family: inherit; font-size: 17px; line-height: normal; overflow: auto; max-width: 100%; color: rgb(34, 34, 34);">>>> data = {'name': 'erik', 'age': 38, 'married': True}
>>> print(json.dumps(data, indent=2))
{
"name": "erik",
"age": 38,
"married": true
}</pre><h2 id="h-pretty-printing-json-with-the-json-module" style="box-sizing: inherit; margin-top: 40px; margin-bottom: 5px; border: 0px; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 35px; font-weight: 400; line-height: 1.2em; color: rgb(34, 34, 34);"><span class="ez-toc-section" id="Pretty_printing_JSON_with_the_JSON_module" style="box-sizing: inherit;"></span><font _mstmutation="1" _msthash="1551615" _msttexthash="48851829" style="box-sizing: inherit;">漂亮的打印杰森与杰森模块</font><span class="ez-toc-section-end" style="box-sizing: inherit;"></span></h2><p _msthash="675516" _msttexthash="272075791" style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;">Python 的 JSON 模块也可以从命令行中使用。它将验证和漂亮打印您的JSON:</p><pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="" style="box-sizing: inherit; margin-bottom: 1.5em; padding: 20px; border: 0px; background: rgba(0, 0, 0, 0.05); font-family: inherit; font-size: 17px; line-height: normal; overflow: auto; max-width: 100%; color: rgb(34, 34, 34);">$ echo "{ \"name\": \"Monty\", \"age\": 45 }" | \
python3 -m json.tool
{
"name": "Monty",
"age": 45
}</pre><p _msthash="675517" _msttexthash="98668687" style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;">您可能也有兴趣使用<font color="#1e73be"><span style="box-sizing: inherit; transition: color 0.1s ease-in-out 0s, background-color 0.1s ease-in-out 0s;">jq工具</span></font>,虽然!</p><h2 id="h-how-to-read-a-json-file-in-python" style="box-sizing: inherit; margin-top: 40px; margin-bottom: 5px; border: 0px; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 35px; font-weight: 400; line-height: 1.2em; color: rgb(34, 34, 34);"><span class="ez-toc-section" id="How_to_read_a_JSON_file_in_python" style="box-sizing: inherit;"></span><font _mstmutation="1" _msthash="1043861" _msttexthash="39693992" style="box-sizing: inherit;">如何在巨蛇中读取 JSON 文件</font><span class="ez-toc-section-end" style="box-sizing: inherit;"></span></h2><p style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;"><font _mstmutation="1" _msthash="675518" _msttexthash="714396124" style="box-sizing: inherit;">此外,还有一个功能称为(没有s)。它将加载来自文件的数据。如果您想将 JSON 文件的内容读入 Python 并解析,请使用以下示例:</font><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">json.loads</code><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">json.load</code></p><pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="" style="box-sizing: inherit; margin-bottom: 1.5em; padding: 20px; border: 0px; background: rgba(0, 0, 0, 0.05); font-family: inherit; font-size: 17px; line-height: normal; overflow: auto; max-width: 100%; color: rgb(34, 34, 34);">with open('data.json') as json_file:
data = json.load(json_file)
...
</pre><h2 id="h-how-to-write-json-to-a-file-in-python" style="box-sizing: inherit; margin-top: 40px; margin-bottom: 5px; border: 0px; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 35px; font-weight: 400; line-height: 1.2em; color: rgb(34, 34, 34);"><span class="ez-toc-section" id="How_to_write_JSON_to_a_file_in_Python" style="box-sizing: inherit;"></span><font _mstmutation="1" _msthash="1261351" _msttexthash="44534412" style="box-sizing: inherit;">如何将 JSON 写到 Python 中的文件</font><span class="ez-toc-section-end" style="box-sizing: inherit;"></span></h2><p style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;"><font _mstmutation="1" _msthash="675519" _msttexthash="53827579" style="box-sizing: inherit;">该功能用于将数据写入 JSON 文件。</font><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">json.dump</code></p><pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="" style="box-sizing: inherit; margin-bottom: 1.5em; padding: 20px; border: 0px; background: rgba(0, 0, 0, 0.05); font-family: inherit; font-size: 17px; line-height: normal; overflow: auto; max-width: 100%; color: rgb(34, 34, 34);">data = {'name': 'Eric', 'age': 38 }
with open('data.json', 'w') as json_file:
json.dump(data, json_file)
</pre><h2 id="h-frequently-asked-questions" style="box-sizing: inherit; margin-top: 40px; margin-bottom: 5px; border: 0px; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 35px; font-weight: 400; line-height: 1.2em; color: rgb(34, 34, 34);"><span class="ez-toc-section" id="Frequently_Asked_Questions" style="box-sizing: inherit;"></span><font _mstmutation="1" _msthash="774371" _msttexthash="25480117" style="box-sizing: inherit;">经常问的问题</font><span class="ez-toc-section-end" style="box-sizing: inherit;"></span></h2><div class="schema-faq wp-block-yoast-faq-block" style="box-sizing: inherit; color: rgb(34, 34, 34); font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 17px;"><div class="schema-faq-section" id="faq-question-1613471240378" style="box-sizing: inherit;"><span class="schema-faq-question" _msthash="444028" _msttexthash="109240378" style="box-sizing: inherit; font-weight: 700;">如何将列表(阵列)转换为Python中的JSON?</span><p class="schema-faq-answer" style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px;"><font _mstmutation="1" _msthash="577694" _msttexthash="113552764" style="box-sizing: inherit;">只需使用上述方法即可。和功能接受字典和列表</font><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">json.dump</code><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">json.dumps</code></p></div><div class="schema-faq-section" id="faq-question-1613471782842" style="box-sizing: inherit;"><span class="schema-faq-question" _msthash="446238" _msttexthash="85416539" style="box-sizing: inherit; font-weight: 700;">如何将口述或字典转换为Python中的JSON?</span><p class="schema-faq-answer" style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px;"><font _mstmutation="1" _msthash="579904" _msttexthash="74622626" style="box-sizing: inherit;">类似于阵列,所以使用或在字典上。</font><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">json.dump </code><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">json.dumps</code></p></div><div class="schema-faq-section" id="faq-question-1613473329282" style="box-sizing: inherit;"><span class="schema-faq-question" _msthash="445237" _msttexthash="98506746" style="box-sizing: inherit; font-weight: 700;">如何对Python中的JSON输出进行排序?</span><p class="schema-faq-answer" style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px;"><font _mstmutation="1" _msthash="578903" _msttexthash="197636322" style="box-sizing: inherit;">转储和转储功能都接受一个称为"sort_keys"的选项,例如: 。</font><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">json.dumps(data, sort_keys=True)</code></p></div><div class="schema-faq-section" id="faq-question-1613473447376" style="box-sizing: inherit;"><span class="schema-faq-question" _msthash="447213" _msttexthash="62571119" style="box-sizing: inherit; font-weight: 700;">JSON库是否输出单码数据?</span><p class="schema-faq-answer" style="box-sizing: inherit; margin-top: 0.5em; margin-bottom: 1.5em; border: 0px;"><font _mstmutation="1" _msthash="580879" _msttexthash="612001507" style="box-sizing: inherit;">默认情况下:没有。库输出 ASCII,并将转换不属于 ASCII 的字符。如果您要输出 Unicode,则将ensure_ascii设置为"错误"。例:</font><code style="box-sizing: inherit; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace;">json.dumps(data, ensure_ascii=False)</code></p></div></div></div>
页:
[1]