diff --git a/addressbook/libebook-contacts/gen-western-table.py b/addressbook/libebook-contacts/gen-western-table.py
index 51baefa..68b1b42 100755
--- a/addressbook/libebook-contacts/gen-western-table.py
+++ b/addressbook/libebook-contacts/gen-western-table.py
@@ -19,30 +19,37 @@
 
 import sys
 
+if sys.version_info[0] >= 3:
+    stdin = sys.stdin.buffer
+    stdout = sys.stdout.buffer
+else:
+    stdin = sys.stdin
+    stdout = sys.stdout
+
 var = None
 strings = []
 
 def output():
-    print "static const gchar %s_table[] = {" % var
+    stdout.write(b"static const gchar " + var + b"_table[] = {\n")
     for s in strings:
-        print "  \"%s\\0\"" % s
-    print "};"
+        stdout.write(b"  \"" + s + b"\\0\"\n")
+    stdout.write(b"};\n")
 
-    print "static const guint %s_index[] = {" % var
+    stdout.write(b"static const guint " + var + b"_index[] = {\n")
     index = 0
     for s in strings:
-        print "  %d," % index
+        stdout.write(b"  " + str(index).encode() + b",\n")
         index += len(s) + 1
-    print "};\n"
+    stdout.write(b"};\n\n")
     
 (S_VAR, S_STRING) = range(0, 2)
 state = S_VAR
 
-print "/* This file is generated by gen-western-table.py. DO NOT EDIT */"
+stdout.write(b"/* This file is generated by gen-western-table.py. DO NOT EDIT */\n")
 
-for l in sys.stdin.readlines():
+for l in stdin.readlines():
     l = l.strip()
-    if l == "":
+    if l == b"":
         state = S_VAR
         output()
         var = None
