我需要计算给定文件中的字符数。问题是,我没有正确地分割文件。如果我的输入文件的内容是“The!dog-ate ##### the,cat”,我不需要输出中的特殊字符。 o/p:t:4 h:2 e:3!:1 d:1 o:1 g:1 - :1#:5 ....此外,我需要删除“ - ”符号并确保该词不连接。不包含特殊字符的字符串中的字符数
from collections import Counter
import sys
filename = sys.argv[1]
reg = '[^a-zA-Z+]'
f = open(filename, 'r')
x = f.read().strip()
lines=[]
for line in x:
line = line.strip().upper()
if line:
lines.append(line)
print(Counter(lines))
有人可以帮助我吗?
如果你的问题得到回答,请[接受最有用的答案(https://stackoverflow.com/help/有人-答案)。 –