1. 程式人生 > 其它 >啟動或暫停所有docker容器

啟動或暫停所有docker容器

簡單描述程式功能:

1.停用詞為csv檔案

2.原始檔為txt檔案

3.文字處理,將原檔案中出現的停用詞去除

程式碼實現:

1.檔案讀取,分詞,原始檔詞頻統計

python 讀取 西班牙語文字編碼:encoding='ISO-8859-1'

 1 #csv 檔案讀取,此處編碼為西班牙語
 2 def csvfile():
 3     file_path = os.path.join(upload_path, "SpanishStopWords.csv")
 4     with open(file_path,'r',encoding='ISO-8859-1') as f:
 5         reader = csv.reader(f)
6 fieldnames = next(reader)#獲取資料的第一列,作為後續要轉為字典的鍵名 生成器,next方法獲取 7 # print(fieldnames) 8 data1=[] 9 csv_reader = csv.DictReader(f,fieldnames=fieldnames) #self._fieldnames = fieldnames # list of keys for the dict 以list的形式存放鍵名 10 for row in csv_reader: 11 dic1={}
12 for k,v in row.items(): 13 dic1[k]=v 14 data1.append(dic1) 15 return data1 16 #txt檔案讀取 17 def eachcount(): 18 file_path = os.path.join(upload_path, "Alamo.txt") 19 txt = open(file_path, 'r', encoding='ISO-8859-1').read() 20 #分詞 21 txt = txt.replace('
,', ' ').replace('.', ' ') 22 txt = txt.split() 23 counts = {} # 定義一個空字典型別 24 print(txt) 25 for word in txt: 26 counts[word] = counts.get(word, 0) + 1 # 獲取word當前有幾個,如果word不存在則為0 27 items = list(counts.items()) 28 # 對一個列表按照鍵值對的兩個元素的第二個元素進行排序,由大到小的倒排,詞頻排序 29 items.sort(key=lambda x: x[1], reverse=False) 30 return items

2.顯示在原檔案中出現的所有停用詞


#顯示在原始檔中出現過的所有停用詞
@application.route('/listsearch/', methods=['GET', 'POST'])
def listsearch():
file_path = os.path.join(upload_path, "SpanishStopWords.csv")
txt = open(file_path, 'r', encoding='ISO-8859-1').read()
txt = txt.split()
filelist=txt
# filelist=csvfile()
filelist2=docu2()
# wordlist=["my","name","boy","chirs","Dave"]
result=[]
result2=[]
# for j in wordlist:
# for i in filelist:
# if i[0]== j :
# result.append(i)
for j in filelist:
for i in filelist2:
if j== i :
result2.append(j)
return render_template('index.html',result2=result2)

前端程式碼展現:
<form action="/listsearch" method="get"  enctype="multipart/form-data">
<button type="submit" value="submit">search</button>

<p>result</p>
{% for line2 in result2 %}
<p>{{ line2}}</p>

{% endfor %}
</form>