一、 序言
今天用Python來試試,對一個檔案里面的英文單詞數量進行快速統計,告別傳統計數方式,
目標檔案
我也不知道多少個,瞎復制的~

二、涉及知識點
- 檔案讀寫
- 基礎語法
- 字串分割
三、代碼實踐
首先我們創建一個Python檔案
匯入我們需要用到的模塊
import platform
# 我給大家準備了這些資料:Python視頻教程、100本Python電子書、基礎、爬蟲、資料分析、web開發、機器學習、人工智能、面試題、Python學習路線圖、問題解答!都放在這個群咯 279199867
代開檔案
with open(filepath, 'r') as fin:
讀取內容
string = fin.read() count += len(string.split(" "))
全部代碼
我加了一些好的東西,大家不要介意,
# 匯入系統包 import platform print("為天地立心,為萬民立命,為往圣繼絕學,為萬世開太平") print("Python統計檔案中單詞數量 ") def stat_words(filepath): count = 0 # 打開檔案 with open(filepath, 'r') as fin: # 讀取內容 string = fin.read() count += len(string.split(" ")) return count print("檔案中單詞數量為:", stat_words("p008.txt")) print("Python 版本", platform.python_version())
檔案目錄
py-008/
├── p008.txt
└── py008.py
四、效果展示
效果duang的一下就出來了,只需一秒,就跟看完不點贊收藏的你一樣快 (???)

好了,兄弟們,今天的分享就到這里,再見!

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/499746.html
標籤:其他
