import base64 import hashlib import os import pandas as pd import django os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' # os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") django.setup() from django.db.models import Count from ck.models import * def hashptid(mrn, hosp='NTUH'): ptsalt = (mrn+hosp).upper().encode() hash_in_bytes = hashlib.md5(ptsalt) md5 = hash_in_bytes.hexdigest() hash = base64.b32encode(hash_in_bytes.digest())[:8].decode() # hash32 = base64.b32encode(hash_in_bytes)[:8].decode() # hash10 = str(int(hashlib.md5(ptsalt).hexdigest(), 16))[-8:] return md5, hash PATIENTS = [] for p in Patient.objects.all().order_by('medical_records'): md5, hash = hashptid(p.medical_records) print (p.medical_records, hash) PATIENTS.append({ 'ChartNo': p.medical_records, 'hash': hash, 'gender': p.gender, 'birthday': p.birthday, }) df = pd.DataFrame(PATIENTS) df.to_excel('hash.xlsx')