Skip to content

Instantly share code, notes, and snippets.

@dada8397
Created March 6, 2021 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dada8397/a04a8e14823b167e2e811f2f1269a1cb to your computer and use it in GitHub Desktop.
Save dada8397/a04a8e14823b167e2e811f2f1269a1cb to your computer and use it in GitHub Desktop.
import json
import collections
import csv
f = open('contacts.json')
c = json.load(f)
# c = [
# {'Id': '0', 'Email': 'Wick', 'Phone': '', 'OrderId': '', 'Contacts': '3'},
# {'Id': '1', 'Email': 'John', 'Phone': '', 'OrderId': '12345678', 'Contacts': '5'},
# {'Id': '8', 'Email': 'Chan', 'Phone': '782212345', 'OrderId': '', 'Contacts': '4'},
# {'Id': '34567', 'Email': '', 'Phone': '682212345', 'OrderId': '12345678', 'Contacts': '2'},
# {'Id': '78999', 'Email': 'Wick', 'Phone': '682212345', 'OrderId': '', 'Contacts': '4'},
# {'Id': '80000', 'Email': 'Dada', 'Phone': '', 'OrderId': '', 'Contacts': '3'},
# {'Id': '80001', 'Email': 'Chan', 'Phone': '', 'OrderId': '87654321', 'Contacts': '5'},
# {'Id': '80002', 'Email': '', 'Phone': '782212345', 'OrderId': '87654321', 'Contacts': '2'},
# ]
# ans = {}
# for cc in c:
# cc['Key'] =
ans = collections.OrderedDict()
for ii in range(500):
anstmp = collections.OrderedDict()
print('Hi' + str(ii))
for cc in c[ii*1000:(ii+1)*1000]:
find = False
for aa in anstmp:
if cc['Email'] in anstmp[aa]['Data'] or cc['Phone'] in anstmp[aa]['Data'] or cc['OrderId'] in anstmp[aa]['Data']:
if cc['Email'] != '':
anstmp[aa]['Data'].add(cc['Email'])
if cc['Phone'] != '':
anstmp[aa]['Data'].add(cc['Phone'])
if cc['OrderId'] != '':
anstmp[aa]['Data'].add(cc['OrderId'])
anstmp[aa]['Contacts'] += int(cc['Contacts'])
anstmp[aa]['Ids'].append(str(cc['Id']))
find = True
break
if not find:
anstmp[int(cc['Id'])] = {'Data': set(), 'Contacts': int(cc['Contacts']), 'Ids': [str(cc['Id'])]}
if cc['Email'] != '':
anstmp[int(cc['Id'])]['Data'].add(cc['Email'])
if cc['Phone'] != '':
anstmp[int(cc['Id'])]['Data'].add(cc['Phone'])
if cc['OrderId'] != '':
anstmp[int(cc['Id'])]['Data'].add(cc['OrderId'])
ans.update(anstmp)
print('Hihi')
# print(ans[0])
ans2 = collections.OrderedDict()
for aa in ans:
find = False
print('Hoho'+str(aa))
for a2 in ans2:
if not ans[aa]['Data'].isdisjoint(ans2[a2]['Data']):
ans2[a2]['Data'].union(ans[aa]['Data'])
ans2[a2]['Contacts'] += ans[aa]['Contacts']
ans2[a2]['Ids'].extend(ans[aa]['Ids'])
ans2[a2]['Ids'] = sorted(ans2[a2]['Ids'])
find = True
break
if not find:
ans2[aa] = ans[aa]
print('Hohoho')
# ans2 = sorted(ans2)
# print(ans2)
real_ans = collections.OrderedDict()
for a in ans2:
for i in ans2[a]['Ids']:
real_ans[int(i)] = {'ticket_trace': '-'.join(sorted(ans2[a]['Ids'])), 'contact': ans2[a]['Contacts']}
# print(sorted(real_ans))
# print(real_ans)
anss = []
for r in sorted(real_ans):
anss.append([r, real_ans[r]['ticket_trace'] + ', ' + str(real_ans[r]['contact'])])
fields = ['ticket_id', 'ticket_trace/contact']
with open('ans2.csv', 'w') as f2:
write = csv.writer(f2)
write.writerow(fields)
write.writerows(anss)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment