def count_non_alphanumeric(filename):
with open(filename, "r") as f: #open the file as f
nonalpha_count=0 # this is the running count of alphanumeric chars
for line in f: #for each line in the file...
for ch in line: #for each character in the line...
if not ch.isalnum(): # check to see if the character is alphanumeric
nonalpha_count+=1
return nonalpha_count
您可以简单地遍历该文件并使用^{} 方法来计算非字母数字字符的数量。在
比如说:
所以在循环之后,
count_special
包含非字母数字字符的总数。在通过一次解析一行,该方法通常能够处理大型文本文件,因为它不必首先将整个文件加载到内存中。在
此函数将返回文件中非字母数字字符的数目:
你可以用这个方法。必须将特殊字符列表定义为
regex
:并以
^{pr2}$re.findall
的长度计算特殊字符数:相关问题 更多 >
编程相关推荐