{"id":1104,"date":"2023-06-11T23:53:11","date_gmt":"2023-06-11T15:53:11","guid":{"rendered":"http:\/\/xinyixx.com\/?p=1104"},"modified":"2023-06-11T23:53:46","modified_gmt":"2023-06-11T15:53:46","slug":"python14","status":"publish","type":"post","link":"https:\/\/www.xinyixx.com\/index.php\/2023\/06\/11\/python14\/","title":{"rendered":"python\u7a0b\u5e8f14\uff1a\u5199\u4e00\u4e2a\u8bcd\u4e91\u7a0b\u5e8f"},"content":{"rendered":"<p>\u6211\u4eec\u7ecf\u5e38\u5728\u7f51\u4e0a\u51b2\u6d6a\u65f6\u5019\u770b\u5230\u8fd9\u6837\u7684\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><noscript><img decoding=\"async\" src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/06\/v2-cd4a62e411a252096976d532dd6a4e8b_r-1024x1024.jpg\" alt class=\"wp-image-1105\" width=\"840\" height=\"840\" srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/v2-cd4a62e411a252096976d532dd6a4e8b_r-1024x1024.jpg 1024w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/v2-cd4a62e411a252096976d532dd6a4e8b_r-300x300.jpg 300w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/v2-cd4a62e411a252096976d532dd6a4e8b_r-150x150.jpg 150w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/v2-cd4a62e411a252096976d532dd6a4e8b_r-768x768.jpg 768w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/v2-cd4a62e411a252096976d532dd6a4e8b_r.jpg 1280w\" sizes=\"(max-width: 840px) 100vw, 840px\"><\/noscript><img decoding=\"async\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20840%20840%22%3E%3C%2Fsvg%3E\" alt class=\"wp-image-1105 lazyload\" width=\"840\" height=\"840\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20840%20840%22%3E%3C%2Fsvg%3E 840w\" sizes=\"(max-width: 840px) 100vw, 840px\" data-srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/v2-cd4a62e411a252096976d532dd6a4e8b_r-1024x1024.jpg 1024w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/v2-cd4a62e411a252096976d532dd6a4e8b_r-300x300.jpg 300w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/v2-cd4a62e411a252096976d532dd6a4e8b_r-150x150.jpg 150w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/v2-cd4a62e411a252096976d532dd6a4e8b_r-768x768.jpg 768w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/06\/v2-cd4a62e411a252096976d532dd6a4e8b_r.jpg 1280w\" data-src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/06\/v2-cd4a62e411a252096976d532dd6a4e8b_r-1024x1024.jpg\"><\/figure>\n\n\n\n<p>\u8fd9\u79cd\u7531\u6c49\u5b57\u51fa\u73b0\u9891\u7387\u591a\u5c11\u7ec4\u6210\u7684\u56fe\u7247\u53eb\u505a\u8bcd\u4e91\u56fe\uff0c\u90a3\u4e48\u8fd9\u6837\u7684\u56fe\u7247\u662f\u600e\u4e48\u751f\u6210\u7684\u5462\uff1f\u8fd9\u91cc\u5c31\u8981\u7528\u5230wordcloud\u6a21\u5757\u4e86\u3002<code>wordcloud<\/code> \u6a21\u5757\u662f\u4e00\u4e2a Python \u5e93\uff0c\u7528\u4e8e\u751f\u6210\u8bcd\u4e91\u3002\u8bcd\u4e91\u662f\u4e00\u79cd\u53ef\u89c6\u5316\u5f62\u5f0f\uff0c\u5176\u4e2d\u6587\u672c\u4e2d\u7684\u5355\u8bcd\u6309\u7167\u9891\u7387\u548c\u91cd\u8981\u6027\u6392\u5217\u5e76\u5448\u73b0\u4e3a\u4e0d\u540c\u5927\u5c0f\u548c\u989c\u8272\u7684\u5355\u8bcd\u4e91\u3002<code>wordcloud<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u9009\u9879\u548c\u53c2\u6570\uff0c\u53ef\u7528\u4e8e\u81ea\u5b9a\u4e49\u8bcd\u4e91\u7684\u5916\u89c2\u548c\u751f\u6210\u65b9\u5f0f\u3002\u6211\u4eec\u4f7f\u7528\u8be5\u6a21\u5757\u6765\u751f\u6210\u6f02\u4eae\u7684\u8bcd\u4e91\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u6587\u672c\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u5728\u7ec8\u7aef\u4e0b\u8f7d\u6a21\u5757\uff1a<code>pip install numpy wordcloud mathplotlib openpyxl<\/code><\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u7a0b\u5e8f\u6e90\u4ee3\u7801\uff0c\u5404\u884c\u6ce8\u91ca\u5df2\u5217\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import numpy as np # numpy\u6570\u636e\u5904\u7406\u5e93\nimport wordcloud # \u8bcd\u4e91\u5e93\nfrom PIL import Image # \u56fe\u50cf\u5904\u7406\u5e93\uff0c\u7528\u4e8e\u8bfb\u53d6\u80cc\u666f\u56fe\u7247\nimport matplotlib.pyplot as plt # \u56fe\u50cf\u5c55\u793a\u5e93\uff0c\u4ee5\u4fbf\u5728notebook\u4e2d\u663e\u793a\u56fe\u7247\nfrom openpyxl import load_workbook #\u8bfb\u53d6\u8bcd\u9891Excel\u6587\u4ef6\nimport os #\u83b7\u53d6\u8bcd\u9891Excel\u6587\u4ef6\u8def\u5f84\n\n#\u5c06\u5b58\u597d\u7684Excel\u8bcd\u9891\u8868\u8bfb\u53d6\u6210\u5b57\u5178\npath='\u8bcd\u9891'  #\u6587\u4ef6\u6240\u5728\u6587\u4ef6\u5939\nfiles = [path+\"\\\\\"+i for i in os.listdir(path)] #\u83b7\u53d6\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6\u540d,\u5e76\u62fc\u63a5\u5b8c\u6574\u8def\u5f84\nmaskImage = np.array(Image.open('background.png')) # \u5b9a\u4e49\u8bcd\u9891\u80cc\u666f\u56fe\n\nfor file in files:\n    #\u5c06\u8bcd\u9891Excel\u6587\u4ef6\u8bfb\u53d6\u4e3a\u5b57\u5178\n    wb = load_workbook(file)\n    ws = wb.active\n    wordFreq = {}\n    for i in range(2,ws.max_row+1):\n        word = ws[\"A\"+str(i)].value\n        freq = ws[\"B\"+str(i)].value\n        wordFreq[word] = freq    \n    \n    #\u5b9a\u4e49\u8bcd\u4e91\u6837\u5f0f\n    wc = wordcloud.WordCloud(\n        font_path='C:\/Windows\/Fonts\/simhei.ttf', # \u8bbe\u7f6e\u5b57\u4f53\n        mask= maskImage, # \u8bbe\u7f6e\u80cc\u666f\u56fe\n        max_words=500, # \u6700\u591a\u663e\u793a\u8bcd\u6570\n        max_font_size=100) # \u5b57\u53f7\u6700\u5927\u503c\n    \n    #\u751f\u6210\u8bcd\u4e91\u56fe\n    wc.generate_from_frequencies(wordFreq) # \u4ece\u5b57\u5178\u751f\u6210\u8bcd\u4e91\n    #\u4fdd\u5b58\u56fe\u7247\u5230\u6307\u5b9a\u6587\u4ef6\u5939\n    wc.to_file(\"\u8bcd\u4e91\u56fe\\\\{}.png\".format(file.split(\"\\\\\")[1][:4]))\n    #\u5728notebook\u4e2d\u663e\u793a\u8bcd\u4e91\u56fe\n    plt.imshow(wc) # \u663e\u793a\u8bcd\u4e91\n    plt.axis('off') # \u5173\u95ed\u5750\u6807\u8f74\n    plt.show() # \u663e\u793a\u56fe\u50cf<\/code><\/pre>\n\n\n\n<p>\u6709\u6761\u4ef6\u7684\u540c\u5b66\u5728IDE\u91cc\u5b9e\u9a8c\u4e00\u4e0b\u5427\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u7ecf\u5e38\u5728\u7f51\u4e0a\u51b2\u6d6a\u65f6\u5019\u770b\u5230\u8fd9\u6837\u7684\u56fe\uff1a \u8fd9\u79cd\u7531\u6c49\u5b57\u51fa\u73b0\u9891\u7387\u591a\u5c11\u7ec4\u6210\u7684\u56fe\u7247\u53eb\u505a\u8bcd\u4e91\u56fe\uff0c\u90a3\u4e48\u8fd9\u6837\u7684\u56fe\u7247\u662f\u600e\u4e48\u751f\u6210\u7684\u5462 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[14,10],"tags":[71,77],"class_list":["post-1104","post","type-post","status-publish","format-standard","hentry","category-teacher","category-coding","tag-python","tag-coding","entry"],"_links":{"self":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts\/1104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/comments?post=1104"}],"version-history":[{"count":0,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts\/1104\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/media?parent=1104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/categories?post=1104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/tags?post=1104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}