from __future__ import print_function
import os

path = os.path.join(os.path.dirname(__file__), "UnicodeData.txt")

f = open(path, "r")
text = f.read()
f.close()

result = []

openValue = None
for line in text.splitlines():
    line = line.split(";")
    value, name, category = line[:3]
    if category in ("Ps", "Pe", "Pi", "Pf"):
        if openValue is None:
            #assert category in ("Ps", "Pi")
            openValue = (value, name, category)
        else:
            #assert category in ("Pe", "Pf")
            if category not in ("Pe", "Pf"):
                result.append("#%s;%s;%s" % (value, name, category))
                result.append("")
            else:
                result.append("%s;%s;%s" % openValue)
                result.append("%s;%s;%s" % (value, name, category))
                result.append("")
                openValue = None

print("\n".join(result))

@@ -313,53 +313,11 @@
 , %22P
-e%22, %22Pi%22, %22Pf%22):%0A        if openValue is None
+i%22)
 :%0A  
@@ -411,79 +411,14 @@
 ry)%0A
-    
     el
-se:%0A            #assert category in (%22Pe%22, %22Pf%22)%0A            
 if c
@@ -429,12 +429,8 @@
 ory 
-not 
 in (
@@ -444,144 +444,44 @@
 Pf%22)
-:%0A                result.append(%22#%25s;%25s;%25s%22 %25 (value, name, category))%0A                result.append(%22%22)%0A            else:%0A     
+ and openValue is not None:%0A
         
    r
@@ -468,35 +468,32 @@
 t None:%0A        
-   
 result.append(%22%25
@@ -514,24 +514,16 @@
 nValue)%0A
-        
         
@@ -582,24 +582,16 @@
         
-   
-     
 result.a
@@ -600,24 +600,16 @@
 end(%22%22)%0A
-        
         

use close neighbors in openClosedUniGenerator.py