{"id":105835,"date":"2026-09-16T09:08:29","date_gmt":"2026-09-16T01:08:29","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/105835.html"},"modified":"2026-09-16T09:08:29","modified_gmt":"2026-09-16T01:08:29","slug":"pe09-python-%e6%96%87%e4%bb%b6%e8%af%bb%e5%86%99%e8%bf%9b%e9%98%b6%ef%bc%9a%e5%90%8c%e6%97%b6%e5%a4%9a%e6%96%87%e4%bb%b6%e4%b8%8e%e6%95%b0%e6%8d%ae%e8%ae%a1%e7%ae%97","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/105835.html","title":{"rendered":"PE09-Python \u6587\u4ef6\u8bfb\u5199\u8fdb\u9636\uff1a\u540c\u65f6\u591a\u6587\u4ef6\u4e0e\u6570\u636e\u8ba1\u7b97"},"content":{"rendered":"<p>\u9002\u7528\u5bf9\u8c61&#xff1a;\u9700\u8981\u505a\u6570\u636e\u8f6c\u6362\u3001\u6587\u4ef6\u6279\u5904\u7406\u3001\u81ea\u52a8\u5316\u62a5\u8868\u7684\u5f00\u53d1\u8005<\/p>\n<p>\u9605\u8bfb\u65f6\u957f&#xff1a;\u7ea6 7 \u5206\u949f<\/p>\n<p>\u5173\u8054\u7ec3\u4e60&#xff1a;pe081-090<\/p>\n<p>\u73af\u5883\u8981\u6c42&#xff1a;Python 3.14&#043;\u3001Jupyter 1.1&#043;\u3001Notebook 7.5&#043;\u3001nbconvert 7.17&#043;<\/p>\n<h3>\u4e00\u53e5\u8bdd\u6838\u5fc3<\/h3>\n<p>with \u4e00\u884c\u6253\u5f00\u591a\u6587\u4ef6 &#043; for line in f \u9010\u884c\u5904\u7406 &#043; f&#034;{x},{y}\\\\n&#034; \u5199\u56de&#xff0c;\u8fd9\u5c31\u662f ETL \u7684\u6838\u5fc3\u5957\u8def\u3002<\/p>\n<hr \/>\n<h3>\u4e00\u3001\u540c\u65f6\u6253\u5f00\u8bfb\u6587\u4ef6\u548c\u5199\u6587\u4ef6<\/h3>\n<p>\u6838\u5fc3&#xff1a; \u7528\u9017\u53f7\u5206\u9694\u591a\u4e2a open&#xff0c;\u7edf\u4e00 with \u7ba1\u7406\u3002<\/p>\n<p><span class=\"token comment\"># \u4e00\u6b21\u6253\u5f00\u4e24\u4e2a\u6587\u4ef6&#xff1a;\u8bfb &#043; \u5199<\/span><br \/>\n<span class=\"token keyword\">with<\/span> <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;input.txt&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> fin<span class=\"token punctuation\">,<\/span> \\\\<br \/>\n     <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;output.txt&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;w&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> fout<span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">for<\/span> line <span class=\"token keyword\">in<\/span> fin<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># \u5904\u7406\u903b\u8f91<\/span><br \/>\n        fout<span class=\"token punctuation\">.<\/span>write<span class=\"token punctuation\">(<\/span>line<span class=\"token punctuation\">)<\/span><\/p>\n<p>\u8981\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u81ea\u52a8 close \u6240\u6709\u6587\u4ef6&#xff0c;\u5373\u4f7f\u4e2d\u95f4\u62a5\u9519<\/li>\n<li>\u8bfb\u7528 &#034;r&#034;&#xff0c;\u5199\u8986\u76d6\u7528 &#034;w&#034;&#xff0c;\u8ffd\u52a0\u7528 &#034;a&#034;<\/li>\n<li>\u5199\u5b8c\u5fc5\u987b fout.close()&#xff08;\u6216\u7528 with&#xff09;&#xff0c;\u5426\u5219\u5185\u5bb9\u53ef\u80fd\u4e22\u5931<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e8c\u3001\u8bfb\u5199\u6a21\u5f0f\u5bf9\u7167<\/h3>\n<table>\n<tr>\u6a21\u5f0f\u542b\u4e49\u6587\u4ef6\u4e0d\u5b58\u5728\u8986\u76d6<\/tr>\n<tbody>\n<tr>\n<td>r<\/td>\n<td>\u53ea\u8bfb<\/td>\n<td>\u62a5\u9519<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>w<\/td>\n<td>\u5199\u5165<\/td>\n<td>\u521b\u5efa<\/td>\n<td>\u662f<\/td>\n<\/tr>\n<tr>\n<td>a<\/td>\n<td>\u8ffd\u52a0<\/td>\n<td>\u521b\u5efa<\/td>\n<td>\u5426<\/td>\n<\/tr>\n<tr>\n<td>r&#043;<\/td>\n<td>\u8bfb\u5199<\/td>\n<td>\u62a5\u9519<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<tr>\n<td>b<\/td>\n<td>\u4e8c\u8fdb\u5236<\/td>\n<td>&#8211;<\/td>\n<td>&#8211;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>w \u6a21\u5f0f\u8b66\u544a&#xff1a; \u6bcf\u6b21\u6253\u5f00\u90fd\u4f1a\u6e05\u7a7a\u539f\u6587\u4ef6&#xff0c;\u6570\u636e\u4e22\u5931\u98ce\u9669\u5927\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u5b9e\u6218&#xff1a;\u6570\u636e\u8f6c\u6362\u5199\u56de\u6587\u4ef6<\/h3>\n<p><span class=\"token keyword\">with<\/span> <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;input.csv&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> fin<span class=\"token punctuation\">,<\/span> \\\\<br \/>\n     <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;output.csv&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;w&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> fout<span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">for<\/span> line <span class=\"token keyword\">in<\/span> fin<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># \u9996\u884c\u662f\u8868\u5934&#xff0c;\u539f\u6837\u5199\u5165<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token string\">&#034;x,y&#034;<\/span> <span class=\"token keyword\">in<\/span> line<span class=\"token punctuation\">:<\/span><br \/>\n            fout<span class=\"token punctuation\">.<\/span>write<span class=\"token punctuation\">(<\/span>line<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">else<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token comment\"># \u6570\u636e\u884c&#xff1a;x,y \u2192 x*5, y*5<\/span><br \/>\n            x<span class=\"token punctuation\">,<\/span> y <span class=\"token operator\">&#061;<\/span> line<span class=\"token punctuation\">.<\/span>strip<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>split<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;,&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            x <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">(<\/span>x<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">5<\/span><br \/>\n            y <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">(<\/span>y<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">5<\/span><br \/>\n            fout<span class=\"token punctuation\">.<\/span>write<span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>x<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">,<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>y<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">\\\\n&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u5173\u952e\u6b65\u9aa4&#xff1a;<\/p>\n<li>line.strip() \u53bb\u6362\u884c\u7b26<\/li>\n<li>.split(&#034;,&#034;) \u6309\u9017\u53f7\u5207\u5206<\/li>\n<li>int() \u8f6c\u6570\u503c\u8ba1\u7b97<\/li>\n<li>f&#034;&#8230;&#034; \u683c\u5f0f\u5316\u8f93\u51fa<\/li>\n<hr \/>\n<h3>\u56db\u3001CSV \u6570\u636e\u7edf\u8ba1\u51fd\u6570<\/h3>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">compute_score<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;\u8ba1\u7b97\u6587\u4ef6\u4e2d\u6240\u6709\u5206\u6570\u7684\u6700\u5927\/\u6700\u5c0f\/\u5e73\u5747\u503c&#034;&#034;&#034;<\/span><br \/>\n    scores <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><br \/>\n    <span class=\"token keyword\">with<\/span> <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;scores.txt&#034;<\/span><span class=\"token punctuation\">,<\/span> encoding<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> fin<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> line <span class=\"token keyword\">in<\/span> fin<span class=\"token punctuation\">:<\/span><br \/>\n            line <span class=\"token operator\">&#061;<\/span> line<span class=\"token punctuation\">.<\/span>strip<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            fields <span class=\"token operator\">&#061;<\/span> line<span class=\"token punctuation\">.<\/span>split<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;,&#034;<\/span><span class=\"token punctuation\">)<\/span>    <span class=\"token comment\"># \u53ea\u652f\u6301\u82f1\u6587\u9017\u53f7<\/span><br \/>\n            scores<span class=\"token punctuation\">.<\/span>append<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">int<\/span><span class=\"token punctuation\">(<\/span>fields<span class=\"token punctuation\">[<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>   <span class=\"token comment\"># \u53d6\u6bcf\u884c\u6700\u540e\u4e00\u4e2a\u5b57\u6bb5<\/span><\/p>\n<p>    max_score <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">max<\/span><span class=\"token punctuation\">(<\/span>scores<span class=\"token punctuation\">)<\/span><br \/>\n    min_score <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">min<\/span><span class=\"token punctuation\">(<\/span>scores<span class=\"token punctuation\">)<\/span><br \/>\n    avg_score <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">round<\/span><span class=\"token punctuation\">(<\/span><span class=\"token builtin\">sum<\/span><span class=\"token punctuation\">(<\/span>scores<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">\/<\/span> <span class=\"token builtin\">len<\/span><span class=\"token punctuation\">(<\/span>scores<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> max_score<span class=\"token punctuation\">,<\/span> min_score<span class=\"token punctuation\">,<\/span> avg_score   <span class=\"token comment\"># \u5143\u7ec4\u591a\u8fd4\u56de\u503c<\/span><\/p>\n<p><span class=\"token comment\"># \u5143\u7ec4\u89e3\u5305<\/span><br \/>\nmax_score<span class=\"token punctuation\">,<\/span> min_score<span class=\"token punctuation\">,<\/span> avg_score <span class=\"token operator\">&#061;<\/span> compute_score<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;\u6700\u9ad8: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>max_score<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">, \u6700\u4f4e: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>min_score<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">, \u5e73\u5747: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>avg_score<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u6700\u9ad8: 99, \u6700\u4f4e: 55, \u5e73\u5747: 80.0<\/span><\/p>\n<p>\u6838\u5fc3 API&#xff1a;<\/p>\n<ul>\n<li>max() \/ min() \/ sum() \/ len()&#xff1a;\u5185\u7f6e\u7edf\u8ba1<\/li>\n<li>round(x, 2)&#xff1a;\u4fdd\u7559 2 \u4f4d\u5c0f\u6570<\/li>\n<li>\u5143\u7ec4\u89e3\u5305&#xff1a;a, b, c &#061; func()<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e94\u3001\u5b57\u7b26\u7f16\u7801<\/h3>\n<p>\u6838\u5fc3&#xff1a; \u5904\u7406\u4e2d\u6587\u5fc5\u987b\u663e\u5f0f\u6307\u5b9a encoding&#061;&#034;utf-8&#034;\u3002<\/p>\n<p><span class=\"token comment\"># \u5199\u6587\u4ef6<\/span><br \/>\n<span class=\"token keyword\">with<\/span> <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;out.txt&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;w&#034;<\/span><span class=\"token punctuation\">,<\/span> encoding<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> f<span class=\"token punctuation\">:<\/span><br \/>\n    f<span class=\"token punctuation\">.<\/span>write<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u4f60\u597d&#xff0c;Python&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># \u8bfb\u6587\u4ef6<\/span><br \/>\n<span class=\"token keyword\">with<\/span> <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;in.txt&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;r&#034;<\/span><span class=\"token punctuation\">,<\/span> encoding<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> f<span class=\"token punctuation\">:<\/span><br \/>\n    content <span class=\"token operator\">&#061;<\/span> f<span class=\"token punctuation\">.<\/span>read<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>Windows \u9ed8\u8ba4\u7f16\u7801&#xff1a; GBK&#xff08;cp936&#xff09;&#xff0c;\u8de8\u5e73\u53f0\u6216\u4e2d\u6587\u6587\u4ef6\u52a1\u5fc5\u7528 UTF-8\u3002<\/p>\n<p>\u67e5\u770b\u6587\u4ef6\u7f16\u7801&#xff08;\u5e38\u7528\u65b9\u6cd5&#xff09;&#xff1a;<\/p>\n<p><span class=\"token keyword\">import<\/span> chardet   <span class=\"token comment\"># \u7b2c\u4e09\u65b9\u5e93<\/span><br \/>\n<span class=\"token keyword\">with<\/span> <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;file.txt&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;rb&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> f<span class=\"token punctuation\">:<\/span><br \/>\n    raw <span class=\"token operator\">&#061;<\/span> f<span class=\"token punctuation\">.<\/span>read<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>chardet<span class=\"token punctuation\">.<\/span>detect<span class=\"token punctuation\">(<\/span>raw<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>   <span class=\"token comment\"># {&#039;encoding&#039;: &#039;UTF-8&#039;, &#039;confidence&#039;: 0.99}<\/span><\/p>\n<hr \/>\n<h3>\u516d\u3001\u6587\u672c vs \u4e8c\u8fdb\u5236\u8bfb\u5199<\/h3>\n<p><span class=\"token comment\"># \u6587\u672c\u6a21\u5f0f<\/span><br \/>\n<span class=\"token keyword\">with<\/span> <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;text.txt&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;r&#034;<\/span><span class=\"token punctuation\">,<\/span> encoding<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> f<span class=\"token punctuation\">:<\/span><br \/>\n    text <span class=\"token operator\">&#061;<\/span> f<span class=\"token punctuation\">.<\/span>read<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>       <span class=\"token comment\"># str \u7c7b\u578b<\/span><\/p>\n<p><span class=\"token comment\"># \u4e8c\u8fdb\u5236\u6a21\u5f0f&#xff08;\u56fe\u7247\u3001\u89c6\u9891\u3001\u538b\u7f29\u5305&#xff09;<\/span><br \/>\n<span class=\"token keyword\">with<\/span> <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;image.jpg&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;rb&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> f<span class=\"token punctuation\">:<\/span><br \/>\n    data <span class=\"token operator\">&#061;<\/span> f<span class=\"token punctuation\">.<\/span>read<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>       <span class=\"token comment\"># bytes \u7c7b\u578b<\/span><\/p>\n<p>\u5173\u952e\u533a\u522b&#xff1a;<\/p>\n<ul>\n<li>\u6587\u672c\u6a21\u5f0f\u9700\u8981 encoding&#xff0c;\u8fd4\u56de str<\/li>\n<li>\u4e8c\u8fdb\u5236\u6a21\u5f0f\u65e0 encoding&#xff0c;\u8fd4\u56de bytes<\/li>\n<li>\u7f51\u7edc\u4e0b\u8f7d\u6587\u4ef6\u5fc5\u987b\u7528 &#034;wb&#034; \u5199\u5165<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e03\u3001\u6587\u4ef6\u6307\u9488\u64cd\u4f5c<\/h3>\n<p><span class=\"token keyword\">with<\/span> <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;file.txt&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;r&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> f<span class=\"token punctuation\">:<\/span><br \/>\n    f<span class=\"token punctuation\">.<\/span>seek<span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span>        <span class=\"token comment\"># \u6307\u9488\u79fb\u5230\u5f00\u5934<\/span><br \/>\n    f<span class=\"token punctuation\">.<\/span>seek<span class=\"token punctuation\">(<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span>       <span class=\"token comment\"># \u79fb\u5230\u7b2c 10 \u5b57\u8282<\/span><br \/>\n    f<span class=\"token punctuation\">.<\/span>tell<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>         <span class=\"token comment\"># \u8fd4\u56de\u5f53\u524d\u6307\u9488\u4f4d\u7f6e<\/span><br \/>\n    f<span class=\"token punctuation\">.<\/span>read<span class=\"token punctuation\">(<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span>        <span class=\"token comment\"># \u8bfb 5 \u4e2a\u5b57\u7b26<\/span><\/p>\n<p>\u5178\u578b\u7528\u9014&#xff1a; \u5927\u6587\u4ef6\u5206\u5757\u8bfb\u53d6\u3001\u65e5\u5fd7\u6587\u4ef6\u5b9a\u4f4d\u3002<\/p>\n<hr \/>\n<h3>\u516b\u3001\u5b9e\u6218&#xff1a;\u6570\u636e\u6e05\u6d17\u6d41\u7a0b<\/h3>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">clean_data<\/span><span class=\"token punctuation\">(<\/span>input_path<span class=\"token punctuation\">,<\/span> output_path<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;\u628a\u810f\u6570\u636e&#xff08;\u7a7a\u884c\/\u6ce8\u91ca\u884c&#xff09;\u8fc7\u6ee4\u540e\u5199\u5165\u65b0\u6587\u4ef6&#034;&#034;&#034;<\/span><br \/>\n    <span class=\"token keyword\">with<\/span> <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span>input_path<span class=\"token punctuation\">,<\/span> encoding<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> fin<span class=\"token punctuation\">,<\/span> \\\\<br \/>\n         <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span>output_path<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;w&#034;<\/span><span class=\"token punctuation\">,<\/span> encoding<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;utf-8&#034;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> fout<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> line <span class=\"token keyword\">in<\/span> fin<span class=\"token punctuation\">:<\/span><br \/>\n            stripped <span class=\"token operator\">&#061;<\/span> line<span class=\"token punctuation\">.<\/span>strip<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token comment\"># \u8df3\u8fc7\u7a7a\u884c\u548c\u6ce8\u91ca\u884c<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> <span class=\"token keyword\">not<\/span> stripped <span class=\"token keyword\">or<\/span> stripped<span class=\"token punctuation\">.<\/span>startswith<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;#&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n                <span class=\"token keyword\">continue<\/span><br \/>\n            fout<span class=\"token punctuation\">.<\/span>write<span class=\"token punctuation\">(<\/span>stripped <span class=\"token operator\">&#043;<\/span> <span class=\"token string\">&#034;\\\\n&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u8ff7\u4f60\u7248 ETL&#xff1a;<\/p>\n<ul>\n<li>Extract&#xff1a;\u8bfb\u6e90\u6587\u4ef6<\/li>\n<li>Transform&#xff1a;\u8fc7\u6ee4\/\u8f6c\u6362<\/li>\n<li>Load&#xff1a;\u5199\u76ee\u6807\u6587\u4ef6<\/li>\n<\/ul>\n<hr \/>\n<h3>\u5b9e\u8df5\u6e05\u5355<\/h3>\n<ul>\n<li>\u5199\u811a\u672c\u7edf\u8ba1 CSV \u6587\u4ef6\u6bcf\u5217\u7684\u6700\u5927\u503c<\/li>\n<li>\u628a\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\u6309\u5173\u952e\u5b57\u8fc7\u6ee4\u540e\u8f93\u51fa<\/li>\n<li>\u5b9e\u73b0&#034;\u8bfb A \u6587\u4ef6&#xff0c;\u8f6c\u5927\u5199&#xff0c;\u5199 B \u6587\u4ef6&#034;\u7684\u529f\u80fd<\/li>\n<li>\u7528 try-except \u5305\u88c5\u6587\u4ef6\u64cd\u4f5c&#xff0c;\u5904\u7406\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u60c5\u51b5<\/li>\n<\/ul>\n<hr \/>\n<h3>\u5e38\u89c1\u95ee\u9898 FAQ<\/h3>\n<p>Q1&#xff1a;with \u6bd4 f &#061; open() \u597d\u5728\u54ea&#xff1f;<\/p>\n<p>A&#xff1a;with \u901a\u8fc7\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4fdd\u8bc1\u6587\u4ef6\u4e00\u5b9a\u5173\u95ed&#xff0c;\u5373\u4f7f\u4e2d\u9014\u5f02\u5e38\u3002\u8fd9\u662f Python \u63a8\u8350\u505a\u6cd5\u3002<\/p>\n<p>Q2&#xff1a;\u8bfb\u6587\u4ef6\u9047\u5230 UnicodeDecodeError \u600e\u4e48\u529e&#xff1f;<\/p>\n<p>A&#xff1a;1&#xff09;\u786e\u8ba4\u6587\u4ef6\u5b9e\u9645\u7f16\u7801&#xff1b;2&#xff09;\u7528 errors&#061;&#034;ignore&#034; \u8df3\u8fc7\u574f\u5b57\u8282&#xff1b;3&#xff09;chardet \u5e93\u81ea\u52a8\u68c0\u6d4b\u3002<\/p>\n<p>Q3&#xff1a;f.read() \/ f.readline() \/ f.readlines() \u533a\u522b&#xff1f;<\/p>\n<p>A&#xff1a;<\/p>\n<ul>\n<li>read()&#xff1a;\u4e00\u6b21\u8bfb\u5168\u90e8<\/li>\n<li>readline()&#xff1a;\u8bfb\u4e00\u884c<\/li>\n<li>readlines()&#xff1a;\u8bfb\u6240\u6709\u884c\u5230\u5217\u8868<\/li>\n<\/ul>\n<p>\u5927\u6587\u4ef6\u4f18\u5148\u7528 for line in f \u9010\u884c\u5904\u7406&#xff0c;\u5185\u5b58\u53cb\u597d\u3002<\/p>\n<hr \/>\n<h3>\u76f8\u5173\u8d44\u6e90<\/h3>\n<ul>\n<li>Python \u5b98\u65b9 &#8211; \u6587\u4ef6\u8bfb\u5199<\/li>\n<li>Python \u5b98\u65b9 &#8211; open \u5185\u7f6e\u51fd\u6570<\/li>\n<\/ul>\n<hr \/>\n<p>\u7f51\u7edc\u53d6\u6750\u6765\u6e90\u4e8e&#xff1a;ant-python-exercises-100p&#xff1a;\u8682\u8681\u5b66 python \u793e\u533a&#xff0c;python \u7f16\u7a0b\u7ec3\u4e60\u9898 100 \u9898<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9002\u7528\u5bf9\u8c61&#xff1a;\u9700\u8981\u505a\u6570\u636e\u8f6c\u6362\u3001\u6587\u4ef6\u6279\u5904\u7406\u3001\u81ea\u52a8\u5316\u62a5\u8868\u7684\u5f00\u53d1\u8005 \u9605\u8bfb\u65f6\u957f&#xff1a;\u7ea6 7 \u5206\u949f \u5173\u8054\u7ec3\u4e60&#xff1a;pe081-090 \u73af\u5883\u8981\u6c42&#xff1a;Python 3.14\u3001Jupyter 1.1\u3001Notebook 7.5\u3001nbconvert 7.17 \u4e00\u53e5\u8bdd\u6838\u5fc3<br \/>\nwith \u4e00\u884c\u6253\u5f00\u591a\u6587\u4ef6  for line in f \u9010\u884c\u5904\u7406  f\\&#8221;{x},{y}\\\\n\\&#8221; \u5199\u56de&#xff0c;\u8fd9\u5c31\u662f ETL \u7684\u6838\u5fc3\u5957\u8def\u3002 \u4e00\u3001\u540c\u65f6\u6253\u5f00\u8bfb\u6587\u4ef6\u548c\u5199\u6587\u4ef6<br \/>\n\u6838\u5fc3&amp;#x<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81],"topic":[],"class_list":["post-105835","post","type-post","status-publish","format-standard","hentry","category-server","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PE09-Python \u6587\u4ef6\u8bfb\u5199\u8fdb\u9636\uff1a\u540c\u65f6\u591a\u6587\u4ef6\u4e0e\u6570\u636e\u8ba1\u7b97 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/105835.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PE09-Python \u6587\u4ef6\u8bfb\u5199\u8fdb\u9636\uff1a\u540c\u65f6\u591a\u6587\u4ef6\u4e0e\u6570\u636e\u8ba1\u7b97 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u9002\u7528\u5bf9\u8c61&#xff1a;\u9700\u8981\u505a\u6570\u636e\u8f6c\u6362\u3001\u6587\u4ef6\u6279\u5904\u7406\u3001\u81ea\u52a8\u5316\u62a5\u8868\u7684\u5f00\u53d1\u8005 \u9605\u8bfb\u65f6\u957f&#xff1a;\u7ea6 7 \u5206\u949f \u5173\u8054\u7ec3\u4e60&#xff1a;pe081-090 \u73af\u5883\u8981\u6c42&#xff1a;Python 3.14\u3001Jupyter 1.1\u3001Notebook 7.5\u3001nbconvert 7.17 \u4e00\u53e5\u8bdd\u6838\u5fc3 with \u4e00\u884c\u6253\u5f00\u591a\u6587\u4ef6 for line in f \u9010\u884c\u5904\u7406 f&quot;{x},{y}\\n&quot; \u5199\u56de&#xff0c;\u8fd9\u5c31\u662f ETL \u7684\u6838\u5fc3\u5957\u8def\u3002 \u4e00\u3001\u540c\u65f6\u6253\u5f00\u8bfb\u6587\u4ef6\u548c\u5199\u6587\u4ef6 \u6838\u5fc3&amp;#x\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/105835.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-16T01:08:29+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/105835.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/105835.html\",\"name\":\"PE09-Python \u6587\u4ef6\u8bfb\u5199\u8fdb\u9636\uff1a\u540c\u65f6\u591a\u6587\u4ef6\u4e0e\u6570\u636e\u8ba1\u7b97 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-16T01:08:29+00:00\",\"dateModified\":\"2026-09-16T01:08:29+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/105835.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/105835.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/105835.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PE09-Python \u6587\u4ef6\u8bfb\u5199\u8fdb\u9636\uff1a\u540c\u65f6\u591a\u6587\u4ef6\u4e0e\u6570\u636e\u8ba1\u7b97\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PE09-Python \u6587\u4ef6\u8bfb\u5199\u8fdb\u9636\uff1a\u540c\u65f6\u591a\u6587\u4ef6\u4e0e\u6570\u636e\u8ba1\u7b97 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/105835.html","og_locale":"zh_CN","og_type":"article","og_title":"PE09-Python \u6587\u4ef6\u8bfb\u5199\u8fdb\u9636\uff1a\u540c\u65f6\u591a\u6587\u4ef6\u4e0e\u6570\u636e\u8ba1\u7b97 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u9002\u7528\u5bf9\u8c61&#xff1a;\u9700\u8981\u505a\u6570\u636e\u8f6c\u6362\u3001\u6587\u4ef6\u6279\u5904\u7406\u3001\u81ea\u52a8\u5316\u62a5\u8868\u7684\u5f00\u53d1\u8005 \u9605\u8bfb\u65f6\u957f&#xff1a;\u7ea6 7 \u5206\u949f \u5173\u8054\u7ec3\u4e60&#xff1a;pe081-090 \u73af\u5883\u8981\u6c42&#xff1a;Python 3.14\u3001Jupyter 1.1\u3001Notebook 7.5\u3001nbconvert 7.17 \u4e00\u53e5\u8bdd\u6838\u5fc3 with \u4e00\u884c\u6253\u5f00\u591a\u6587\u4ef6 for line in f \u9010\u884c\u5904\u7406 f\"{x},{y}\\n\" \u5199\u56de&#xff0c;\u8fd9\u5c31\u662f ETL \u7684\u6838\u5fc3\u5957\u8def\u3002 \u4e00\u3001\u540c\u65f6\u6253\u5f00\u8bfb\u6587\u4ef6\u548c\u5199\u6587\u4ef6 \u6838\u5fc3&amp;#x","og_url":"https:\/\/www.wsisp.com\/helps\/105835.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-16T01:08:29+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/105835.html","url":"https:\/\/www.wsisp.com\/helps\/105835.html","name":"PE09-Python \u6587\u4ef6\u8bfb\u5199\u8fdb\u9636\uff1a\u540c\u65f6\u591a\u6587\u4ef6\u4e0e\u6570\u636e\u8ba1\u7b97 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-16T01:08:29+00:00","dateModified":"2026-09-16T01:08:29+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/105835.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/105835.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/105835.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"PE09-Python \u6587\u4ef6\u8bfb\u5199\u8fdb\u9636\uff1a\u540c\u65f6\u591a\u6587\u4ef6\u4e0e\u6570\u636e\u8ba1\u7b97"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/105835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=105835"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/105835\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=105835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=105835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=105835"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=105835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}