9.6. Handling Line Ends

One small problem remains. Note the \n strings in the above output. These are literally the two characters “\” followed by “n” as the PYX format reports line ends in this escaped form. Before creating an xTree, we need to decode these into real line ends. The decode function below does the work.

CD-ROM reference=9020.txt
def Decode(s):
      return string.replace (s,"\\n","\n"))

We now need to call this function before creating any new xData nodes.

The PYX2String method:

CD-ROM reference=9021.txt
                  elif L[0] == '_':
                        datum = xData(l[1:])

CD-ROM reference=9022.txt
                  elif L[0] == '_':
                        datum = xData(Decode(l[1:]))

We now get correct line ends in the regenerated XML created by the test harness.

CD-ROM reference=9023.txt
C>python ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.