[英]What is the best way of writing the number of occurrences of a word in a file for multiple files and writing the results to another file
[英]Writing number of occurrences to a file
我正在計算列表(稱為a_master)中單詞出現的次數。 要搜索和計數的單詞在dictionary.txt中。 問題是,當我將計數寫入文件時,結果如下:
1Count cloud
19Count openstack
3
這是代碼:
with open("dictionary.txt","r") as f:
for line in f:
if a_master.count(line.strip()) !=0:
file.write( "Count " + line + str((a_master).count(line.strip())))
如您所見,由於某種原因,當輸出數字時,它會將其放在新行上,我不知道為什么!
在線使用.strip()。
嘗試這個
with open("dictionary.txt","r") as f:
for line in f:
if a_master.count(line.strip()) !=0:
file.write( "Count " + line.strip() + str((a_master).count(line.strip())))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.