[英]What is the most efficient way to find all files containing given string, case-insensitive using Python 3
[英]How to find all occurences of string (case-insensitive) in a file, python?
我有以下代碼來找到文件中的字符串並計算完全匹配的次數:
counter+=book.read().count(word)
我想做的是計算單詞(存儲在變量單詞中-這是for循環的一部分)出現的次數,但不區分大小寫。
有誰知道如何做到這一點?
將兩個字符串都轉換為小寫:
counter+=book.read().lower().count(word.lower())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.