with open('ip.txt', 'r') as fr:ip = fr.read().split()l = []temp = {}for s in set(ip): temp['ip'] = s temp['count'] = 0 for i in ip: if s == i: temp['count'] += 1 with open('a.json', 'a') as fw: fw.write(str(temp)+'\n')