{"id":150169,"date":"2020-09-02T20:09:43","date_gmt":"2020-09-02T12:09:43","guid":{"rendered":"http:\/\/4563.org\/?p=150169"},"modified":"2020-09-02T20:09:43","modified_gmt":"2020-09-02T12:09:43","slug":"python-xml-%e6%a8%a1%e5%9d%97%e7%9a%84%e4%b8%80%e4%b8%aa%e5%9d%91","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=150169","title":{"rendered":"Python xml \u6a21\u5757\u7684\u4e00\u4e2a\u5751"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python xml \u6a21\u5757\u7684\u4e00\u4e2a\u5751               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Leon6868 <\/span>  <span><i><\/i> 19<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u8fd9\u4e2a\u5751\u51fa\u5728\u6587\u4ef6\u7f16\u7801\u4e0a\u3002<br \/> \u5f53\u4f60\u4f7f\u7528 <code>doc.writexml(f, encoding=\"utf-8\")<\/code> \u65f6\uff0c\u8fd9\u91cc\u7684 encoding \u53ea\u4f1a\u5728 xml \u6807\u7b7e\u4e0a\u6dfb\u52a0<code>encoding=\"utf-8\"<\/code>\u6807\u7b7e\uff0c\u800c xml \u6587\u4ef6\u5185\u5bb9\u672c\u8eab\u7684\u7f16\u7801\uff0c\u662f\u6ca1\u6709\u6539\u53d8\u7684\u3002<br \/> \u8fd9\u5c31\u610f\u5473\u7740\uff0c\u5982\u679c\u4f60\u5199\u5165\u4e2d\u6587\uff0c\u65e0\u8bba\u4f60\u600e\u4e48 encode \u3001decode\uff0c\u6700\u7ec8\u53ea\u4f1a\u5f97\u5230\u4e00\u4e2a\u5145\u6ee1\u4e71\u7801\u7684 xml \u6587\u4ef6\u3002<br \/> \u6b63\u786e\u7684\u5199\u6cd5\u662f\u8fd9\u6837\uff1a<\/p>\n<pre><code>with open(file, \"w\", encoding=\"utf-8\") as f:  \u00a0 \u00a0doc.writexml(file, encoding=\"utf-8\") <\/code><\/pre>\n<p>\u5728 open \u65b9\u6cd5\u91cc\u9762\u52a0\u5165 encoding=&#8221;utf-8&#8243;\uff0c\u624d\u4f1a\u6539\u53d8\u6587\u4ef6\u7684\u7f16\u7801\u3002<\/p>\n<p> \u8fd9\u4e2a\u5751\u8fd8\u6ca1\u6709\u4eba\u4e13\u95e8\u5199\uff0c\u6240\u4ee5\u8bb0\u5f55\u4e00\u4e0b\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4e00\u4e9b\u4eba\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2976402\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : learningman <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bf4\u660e\u770b\u6587\u6863\u8981\u4ed4\u7ec6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2976403\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jirajine <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Python \u7684\u8fd9\u4e2a\u7f16\u7801\u95ee\u9898\u4e00\u76f4\u90fd\u5f88\u5751\uff0c\u522b\u7684\u5927\u591a\u6570\u8bed\u8a00\u5b57\u7b26\u4e32\u90fd\u662f utf-8 encoded bytes\uff0c\u552f\u72ec Python \u7684\u5b57\u7b26\u4e32\u662f\u672a\u7f16\u7801\u7684\uff0c\u800c\u4e14\u7f16\u7801\u65f6\u9ed8\u8ba4\u8fd8\u662f\u5e73\u53f0\u7684\u7f16\u7801\u800c\u4e0d\u662f\u4e00\u5f8b utf-8 \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2976404\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Nitroethane <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u6015\u662f\u4f60\u6ca1\u4ed4\u7ec6\u770b\u6587\u6863\u5427\uff0c\u6587\u6863\u91cc\u6e05\u695a\u5730\u5199\u4e86 \u201cFor the Document node, an additional keyword argument encoding can be used to specify the encoding field of the XML header.\u201d\uff0c\u4eba\u53ef\u6ca1\u8bf4\u662f\u6307\u5b9a\u6587\u4ef6\u5185\u5bb9\u7684\u7f16\u7801                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2976405\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : abersheeran <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u4e2a\u4eba\u89c9\u5f97\u662f\u4f60\u4e0d\u61c2 open \u8fd9\u4e2a\u5185\u7f6e\u51fd\u6570\u4ee5\u53ca\u6587\u4ef6\u7f16\u7801\u5bfc\u81f4\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2976406\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : abersheeran <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Jirajine \u5144\u5f1f\uff0c\u8fd9\u90fd 2020 \u5e74\u4e86\uff0c\u4f60\u8fd8\u5728\u8fd9\u5ff5 Python2 \u7684\u7ecf\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2976407\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fhsan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u90fd\u4ec0\u4e48\u5e74\u4ee3\u4e86\uff0c\u8fd8\u5206\u4e0d\u6e05\u7f16\u7801\u95ee\u9898\uff0cpython \u83b7\u53d6\u5f53\u524d\u64cd\u4f5c\u7cfb\u7edf\u7f16\u7801\uff0c\u5982\u679c\u4f60\u60f3\u4fdd\u6301\u4e00\u81f4\uff0cwrite \u3001open \u6587\u672c\u6587\u4ef6\u90fd\u8981\u6307\u5b9a\u7f16\u7801\u3002\u7edf\u4e00\u7f16\u7801\u662f\u4e0d\u53ef\u80fd\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2976408\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jirajine <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @abersheeran \u8bf4\u7684\u5c31\u662f Python3 \u554a\uff0c\u522b\u7684\u8bed\u8a00 string \u53ef\u4ee5\u76f4\u63a5\u5f53\u6210 utf-8 encoded bytes \u7528\uff0c\u552f\u72ec Python \u9700\u8981\u989d\u5916\u628a string encode \u6210 bytes\uff0c\u5e76\u4e14\u9ed8\u8ba4\u53c2\u6570\u5728 Windows \u4e0b\u8fd8\u4e0d\u662f utf-8 \u3002<br \/>\u81f3\u4e8e\u5185\u90e8\u5982\u4f55\u8868\u793a\u90a3\u662f\u53e6\u4e00\u56de\u4e8b\u4e86\uff0cstring \u66b4\u9732\u51fa\u6765\u7684\u63a5\u53e3\u5c31\u662f\u672a\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u9700\u8981\u8c03\u7528 encode()\u624d\u4f1a\u53d8\u6210 bytes \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2976409\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : abersheeran <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Jirajine \u4f60\u8bf4\u7684\u522b\u7684\u8bed\u8a00\u6015\u662f C \u8fd9\u79cd\u5f31\u7c7b\u578b\u5427\u3002\u54ea\u4e2a\u5f3a\u7c7b\u578b\u8bed\u8a00\u8fd8\u80fd\u628a string \u5f53 bytes \u7528\u7684\uff1f\u4f60\u4e3e\u4e2a\u4f8b\u5b50\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2976410\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jirajine <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @abersheeran golong \u3001rust \u90fd\u662f\u8fd9\u6837\u3002<br \/>\u6211\u4e0a\u9762\u7684\u63cf\u8ff0\u4e0d\u592a\u51c6\u786e\uff0c\u4e0d\u662f\u628a string \u5f53 bytes \u7528\uff0c\u800c\u662f string \u5c31\u662f utf-8 encoded bytes \u3002<br \/>\u56e0\u6b64\u628a string \u5f80\u6587\u4ef6\u5199\u4e0d\u9700\u8981\u6307\u5b9a\u7f16\u7801\uff0cstring \u5c31\u662f\u4e8c\u8fdb\u5236\u7684 bytes\uff0c\u5199\u4e8c\u8fdb\u5236\u6570\u636e\u5f53\u7136\u4e0d\u9700\u8981\u6307\u5b9a\u7f16\u7801\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python xml \u6a21\u5757\u7684\u4e00\u4e2a\u5751&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/150169"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=150169"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/150169\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=150169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=150169"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=150169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}