{"id":937,"date":"2024-11-07T10:17:20","date_gmt":"2024-11-07T02:17:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/937\/"},"modified":"2024-11-07T10:17:20","modified_gmt":"2024-11-07T02:17:20","slug":"python%e7%88%ac%e8%99%ab%e6%80%8e%e4%b9%88%e8%be%93%e5%87%ba%e7%bb%93%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/937\/","title":{"rendered":"Python\u722c\u866b\u600e\u4e48\u8f93\u51fa\u7ed3\u679c"},"content":{"rendered":"<blockquote><p>\n  python \u722c\u866b\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8f93\u51fa\u6293\u53d6\u7ed3\u679c\uff1a\u63a7\u5236\u53f0\u8f93\u51fa\uff1a\u76f4\u63a5\u6253\u5370\u7ed3\u679c\u5230\u63a7\u5236\u53f0\u3002\u6587\u4ef6\u8f93\u51fa\uff1a\u5c06\u7ed3\u679c\u5199\u5165\u7279\u5b9a\u6587\u4ef6\u3002\u6570\u636e\u5e93\u8f93\u51fa\uff1a\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u3002json \u8f93\u51fa\uff1a\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a json \u683c\u5f0f\u3002csv \u8f93\u51fa\uff1a\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a csv \u683c\u5f0f\u3002\n<\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1044\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024110604130533251.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024110604130533251.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024110604130533251-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024110604130533251-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024110604130533251-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"Python\u722c\u866b\u600e\u4e48\u8f93\u51fa\u7ed3\u679c\u63d2\u56fe\" alt=\"Python\u722c\u866b\u600e\u4e48\u8f93\u51fa\u7ed3\u679c\u63d2\u56fe\" \/><\/p>\n<p><strong>Python \u722c\u866b\u8f93\u51fa\u7ed3\u679c\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>Python\u722c\u866b\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8f93\u51fa\u6293\u53d6\u5230\u7684\u7ed3\u679c\uff0c\u5305\u62ec\uff1a<\/p>\n<p><strong>1. \u63a7\u5236\u53f0\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>print(results)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u6587\u4ef6\u8f93\u51fa\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b<\/p>\n<pre>with open(\"output.txt\", \"w\") as f:\n    f.write(results)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u6570\u636e\u5e93\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>import pymongo\n\nclient = pymongo.MongoClient(\"mongodb:\/\/localhost:27017\")\ndb = client[\"my_database\"]\ncollection = db[\"my_collection\"]\n\ncollection.insert_one(results)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. JSON \u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>import json\n\njson_string = json.dumps(results)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. CSV \u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>import csv\n\nwith open(\"output.csv\", \"w\", newline='') as f:\n    writer = csv.writer(f)\n    writer.writerows(results)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u8f93\u51fa\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u6240\u9009\u7684\u8f93\u51fa\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u683c\u5f0f\u3001\u5927\u5c0f\u548c\u76ee\u7684\u3002<\/p>\n<ul>\n<li> <strong>\u63a7\u5236\u53f0\u8f93\u51fa\uff1a<\/strong>\u9002\u7528\u4e8e\u8c03\u8bd5\u548c\u5feb\u901f\u67e5\u770b\u7ed3\u679c\u3002<\/li>\n<li> <strong>\u6587\u4ef6\u8f93\u51fa\uff1a<\/strong>\u9002\u7528\u4e8e\u6c38\u4e45\u5b58\u50a8\u548c\u5206\u6790\u3002<\/li>\n<li> <strong>\u6570\u636e\u5e93\u8f93\u51fa\uff1a<\/strong>\u9002\u7528\u4e8e\u5927\u6570\u636e\u96c6\u7684\u7ed3\u6784\u5316\u5b58\u50a8\u548c\u68c0\u7d22\u3002<\/li>\n<li> <strong>JSON \u8f93\u51fa\uff1a<\/strong>\u9002\u7528\u4e8e\u4e0e\u5176\u4ed6\u7a0b\u5e8f\u6216 API \u4ea4\u6362\u6570\u636e\u3002<\/li>\n<li> <strong>CSV \u8f93\u51fa\uff1a<\/strong>\u9002\u7528\u4e8e\u4e0e\u7535\u5b50\u8868\u683c\u6216\u6570\u636e\u5206\u6790\u5de5\u5177\u517c\u5bb9\u7684\u6570\u636e\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fPython\u722c\u866b\u600e\u4e48\u8f93\u51fa\u7ed3\u679c\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>python \u722c\u866b\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8f93\u51fa\u6293\u53d6\u7ed3\u679c\uff1a\u63a7\u5236\u53f0\u8f93\u51fa\uff1a\u76f4\u63a5\u6253\u5370\u7ed3\u679c\u5230\u63a7\u5236\u53f0\u3002\u6587\u4ef6\u8f93\u51fa\uff1a\u5c06\u7ed3\u679c\u5199\u5165\u7279\u5b9a\u6587\u4ef6\u3002\u6570\u636e\u5e93\u8f93\u51fa\uff1a\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u3002json \u8f93\u51fa\uff1a\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a json \u683c\u5f0f\u3002csv \u8f93\u51fa\uff1a\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a csv \u683c\u5f0f\u3002 Python \u722c\u866b\u8f93\u51fa\u7ed3\u679c\u7684\u65b9\u6cd5 Python\u722c\u866b\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8f93\u51fa\u6293\u53d6\u5230\u7684\u7ed3\u679c\uff0c\u5305\u62ec\uff1a 1. \u63a7\u5236\u53f0\u8f93\u51fa\uff1a print(results) \u767b\u5f55\u540e\u590d\u5236 2. \u6587\u4ef6\u8f93\u51fa\uff1a \u7acb\u5373\u5b66\u4e60\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b with open(&#8220;output.txt&#8221;, &#8220;w&#8221;) as f: f.write(results) \u767b\u5f55\u540e\u590d\u5236 3. \u6570\u636e\u5e93\u8f93\u51fa\uff1a import pymongo client = pymongo.MongoClient(&#8220;mongodb:\/\/localhost:27017&#8221;) db = client[&#8220;my_database&#8221;] collection = db[&#8220;my_collection&#8221;] collection.insert_one(results) \u767b\u5f55\u540e\u590d\u5236 4. JSON \u8f93\u51fa\uff1a import json json_string = json.dumps(results) \u767b\u5f55\u540e\u590d\u5236 5. CSV \u8f93\u51fa\uff1a import csv with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-937","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=937"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/937\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}