diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py
index dbafd47d52aec3d1767ccca98e29633ac9bf8df3..c5ad708e891249771cfdd43a29954f72115e4d73 100644
--- a/py/makeqstrdata.py
+++ b/py/makeqstrdata.py
@@ -1,6 +1,12 @@
 import argparse
 import re
-from htmlentitydefs import codepoint2name
+
+# codepoint2name is different in Python 2 to Python 3
+import platform
+if platform.python_version_tuple()[0] == '2':
+    from htmlentitydefs import codepoint2name
+elif platform.python_version_tuple()[0] == '3':
+    from html.entities import codepoint2name
 
 # this must match the equivalent function in qstr.c
 def compute_hash(qstr):
@@ -37,13 +43,13 @@ def do_work(infiles):
                 if ident in qstrs:
                     continue
 
-                # add the qstr to the list
-                qstrs[ident] = qstr
+                # add the qstr to the list, with order number to retain original order in file
+                qstrs[ident] = (len(qstrs), ident, qstr)
 
     # process the qstrs, printing out the generated C header file
     print('// This file was automatically generated by makeqstrdata.py')
     print('')
-    for ident, qstr in qstrs.items():
+    for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
         qhash = compute_hash(qstr)
         qlen = len(qstr)
         print('Q({}, (const byte*)"\\x{:02x}\\x{:02x}\\x{:02x}\\x{:02x}" "{}")'.format(ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qstr))