赞
踩
- import jieba
- import wordcloud
- import imageio
-
- def Wordcloud(word,outFile,pic):
- maskImage=imageio.v2.imread(pic)
- wc=wordcloud.WordCloud(font_path='simhei.ttf',mask=maskImage,background_color='red')
- wc.generate(' '.join(word))
- wc.to_file(outFile)
-
- def readwords(file,stop=''):
- if stop=='':
- sp=[]
- else:
- sp=stopwords(stop)
- with open(file,'r',encoding='UTF-8')as f:
- x=f.read()#是str
- words=[]
- for w in jieba.lcut(x):
- w=w.strip()
- if not w:
- continue
- if w in sp:
- continue
- words.append(w)
- return words
-
- def stopwords(file):
- sp=[line.strip() for line in open(file,'r',encoding='UTF-8').readlines()]
- print(sp)
- return sp
-
-
- def main():
- ws=readwords('二十大报告全文.txt','stops.txt')
- pic="123.bmp"
- Wordcloud(ws,'imageoutFile.png',pic)
-
-
-
-
- if __name__=='__main__':
- main()
-
词云图代码,如上,要使用该代码请注意:
文章位置,若与代码文件地址不同请r+‘完整地址
’图片位置
需要更换背景颜色请看wordcould函数内该行代码,如下:
请自行改动需调用的内容
- def garde(file):
- with open(file,'r',encoding='UTF-8')as fp:
- Ft=fp.readline()
- Ft=Ft.split()
- ls=[]
- for line in fp.readlines():
- lin=line.split()
- student={}
- cj={}
- for i in range(len(lin)):
- if i<2:
- student.update({Ft[i]:lin[i]})
- else:
- cj.update({Ft[i]:lin[i]})
- student.update({'成绩':cj})
- ls.append(student)
- return ls
-
-
- def 学分(file):
- with open(file,'r',encoding='cp936')as fp:
- dic={}
- for line in fp.readlines():
- line=line.replace('\n','')
- line=line.split(',')
- dic.update({line[0]:line[1]})
- return dic
-
- def 算分(fenshu,xuefen):
- 课程学分=eval(xuefen)
- 课程分数=eval(fenshu)
- if 课程分数<60:
- 课程绩点=0
- else:
- 课程绩点=课程分数/10-5
- 学分绩点=课程绩点*课程学分
- return 学分绩点
-
- def GPA(xuefenjidianbiao,xuefenhe):
- GPA=[]
- for i in xuefenjidianbiao:
- x=0
- for j in i:
- x+=j
- gpa=x/xuefenhe
- GPA.append(round(gpa,2))
- return GPA
-
- def 学分绩点():
- y=[]
- ls=garde('scores.txt')
- for c in ls:
- x=[]
- cj=c['成绩']
- cj=list(cj.values())
-
- xuefen=学分('courses.csv')
- xuefen=list(xuefen.values())
- xuefen=xuefen[1:]
- for i in range(len(cj)):
- x.append(round(算分(cj[i],xuefen[i]),2))
- y.append(x)
- return y
-
- def main():
- ls=garde('scores.txt')
- xuefen=list(学分('courses.csv').values())
- xuefen=xuefen[1:]
- xuefenhe=sum(map(int,xuefen))
- xj=学分绩点()
- y=GPA(xj,xuefenhe)
- pGPA=round(sum(y)/len(y),2)
- for i in range(len(ls)):
- ls[i].update({'GPA':y[i]})
- ls.sort(key=lambda x:x['GPA'],reverse=True)
- return ls[0]['姓名'],ls[-1]['姓名'],pGPA
-
-
-
- if '__main__'==__name__:
- print(main())
GPA代码,如上,更改该代码请注意:函数间调用关系。
此为拙作还请谅解
- def readFile(file):
- '''此函数用于读取文本文件内容
- para:file,指定读取文件
- return:list,读取的数据可以格式化表示为list/dict返回'''
- with open(file,'r',encoding='UTF-8')as f:
- dic={}
- for line in f.readlines()[1:]:
- line=line.split()
- dic[line[0]]=line[1]
- return dic
-
- def mergeData(edata, tdata):
- '''此函数用于合并email和telephone信息,以人名为关键词进行合并,没有可以为空
- para:edata,待合并的email数据
- para:tdata,待合并的telephone数据
- return:list,读取的数据格式化表示为list返回'''
- e=set(edata.keys())
- t=set(tdata.keys())
- s=e|t
- ls=list(s)
- print(ls)
- print(type(tdata))
- tp2=()
- ls3=[]
- for i in range(len(ls)):
- if ls[i] not in edata:
- edata[ls[i]]=' '
-
- if ls[i] not in tdata:
- tdata[ls[i]]=' '
- ls3.append((ls[i],edata[ls[i]],tdata[ls[i]],'\n'))
- print(ls3)
- return ls3
-
-
-
-
- def writeFile(data, file):
- '''此函数用于将合并后的联系方式写入文件
- para:data,写入数据,遍历逐行写入文件
- para:file,指定写入文件
- return:写文件操作无需设置返回值'''
- with open(file,'w',encoding='UTF-8')as f:
- for i in data:
- f.writelines(i)
-
-
- def main():
- emaildata = readFile('emailBook.txt')
- teldata = readFile('telBook.txt')
- mdata = mergeData(emaildata, teldata)
- writeFile(mdata, 'contectBook.txt')
-
- if __name__=='__main__':
- print(main())
此为合并内容代码,如上
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。