人生苦短,我學Python-007:檔案和資料格式化
阿新 • • 發佈:2019-01-10
#AutoTraceDraw.py import turtle as t t.title('自動軌跡繪製') t.setup(800, 600, 0, 0) t.pencolor("red") t.pensize(5) #資料讀取 datals = [] f = open("data.txt") for line in f: line = line.replace("\n","") datals.append(list(map(eval, line.split(",")))) f.close() #自動繪製 for i in range(len(datals)): t.pencolor(datals[i][3//////////////////],datals[i][4],datals[i][5]) t.fd(datals[i][0]) if datals[i][1]: t.rt(datals[i][2]) else: t.lt(datals[i][2])
data.txt
//////////////////
300,0,144,1,0,0
300,0,144,0,1,0
300,0,144,0,0,1
300,0,144,1,1,0
300,0,108,0,1,1
184,0,72,1,0,1
184,0,72,0,0,0
184,0,72,0,0,0
184,0,72,0,0,0
184,1,72,1,0,1
184,1,72,0,0,0
184,1,72,0,0,0
184,1,72,0,0,0
184,1,72,0,0,0
184,1,720,0,0,0
#GovRptWordCloudv1.py import jieba import wordcloud f = open("新時代中國特色社會主義.txt", "r", encoding="utf-8") t = f.read() f.close() ls = jieba.lcut(t) txt = " ".join(ls) w = wordcloud.WordCloud( \ width = 1000, height = 700,\ background_color = "white", font_path = "msyh.ttc" ) w.generate(txt) w.to_file("grwordcloud.png")
#GovRptWordCloudv1.py import jieba import wordcloud f = open("關於實施鄉村振興戰略的意見.txt", "r", encoding="utf-8") t = f.read() f.close() ls = jieba.lcut(t) txt = " ".join(ls) w = wordcloud.WordCloud( \ width = 1000, height = 700,\ background_color = "white", font_path = "msyh.ttc" ) w.generate(txt) w.to_file("grwordcloud.png")
#GovRptWordCloudv1.py import jieba import wordcloud f = open("新時代中國特色社會主義.txt", "r", encoding="utf-8") t = f.read() f.close() ls = jieba.lcut(t) txt = " ".join(ls) w = wordcloud.WordCloud( \ width = 1000, height = 700,\ background_color = "white", font_path = "msyh.ttc", max_words = 15 ) w.generate(txt) w.to_file("grwordcloud.png")
#GovRptWordCloudv1.py import jieba import wordcloud f = open("關於實施鄉村振興戰略的意見.txt", "r", encoding="utf-8") t = f.read() f.close() ls = jieba.lcut(t) txt = " ".join(ls) w = wordcloud.WordCloud( \ width = 1000, height = 700,\ background_color = "white", font_path = "msyh.ttc", max_words = 15 ) w.generate(txt) w.to_file("grwordcloud.png")
#GovRptWordCloudv2.py import jieba import wordcloud from scipy.misc import imread mask = imread("fivestart.png") excludes = { } f = open("新時代中國特色社會主義.txt", "r", encoding="utf-8") t = f.read() f.close() ls = jieba.lcut(t) txt = " ".join(ls) w = wordcloud.WordCloud(\ width = 1000, height = 700,\ background_color = "white", font_path = "msyh.ttc", mask = mask ) w.generate(txt) w.to_file("grwordcloudm.png")
#GovRptWordCloudv2.py import jieba import wordcloud from scipy.misc import imread mask = imread("fivestart.png") excludes = { } f = open("關於實施鄉村振興戰略的意見.txt", "r", encoding="utf-8") t = f.read() f.close() ls = jieba.lcut(t) txt = " ".join(ls) w = wordcloud.WordCloud(\ width = 1000, height = 700,\ background_color = "white", font_path = "msyh.ttc", mask = mask ) w.generate(txt) w.to_file("grwordcloudm.png")
#GovRptWordCloudv2.py import jieba import wordcloud from scipy.misc import imread mask = imread("chinamap.jpg") excludes = { } f = open("新時代中國特色社會主義.txt", "r", encoding="utf-8") t = f.read() f.close() ls = jieba.lcut(t) txt = " ".join(ls) w = wordcloud.WordCloud(\ width = 1000, height = 700,\ background_color = "white", font_path = "msyh.ttc", mask = mask ) w.generate(txt) w.to_file("grwordcloudm.png")
#GovRptWordCloudv2.py import jieba import wordcloud from scipy.misc import imread mask = imread("chinamap.jpg") excludes = { } f = open("關於實施鄉村振興戰略的意見.txt", "r", encoding="utf-8") t = f.read() f.close() ls = jieba.lcut(t) txt = " ".join(ls) w = wordcloud.WordCloud(\ width = 1000, height = 700,\ background_color = "white", font_path = "msyh.ttc", mask = mask ) w.generate(txt) w.to_file("grwordcloudm.png")