# coding = utf-8
import re
with open('D://1/1.txt','r',encoding='utf-8') as f:
line = f.read().strip()
# strip:去除首尾空格
result = re.split(r"[\s\n]", line)
# \s:从左往右 \n:换行
# print(result)
ip_count = {}
for ip in result:
if ip not in ip_count:
ip_count[ip] = 1
else:
ip_count[ip] += 1
if __name__ == '__main__':
print(ip_count,end='\n')
注:我这是在一个文本了打开的,当然,你们可以灵活变通,把这用其他地方。
注:本文借鉴https://www.jb51.net/article/69011.htm,如有冒犯或错误,请及时告知。Thanks♪(・ω・)ノ!