繁体   English   中英

如何找到文件python中所有出现的字符串(不区分大小写)?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM