Handling `
` in Python

Viewed 1051

Problem Background:

I have an XML file that I'm importing into BeautifulSoup and parsing through. One node has the following:

<DIAttribute name="ObjectDesc" value="Line1&#xD;&#xA;Line2&#xD;&#xA;Line3"/>

Notice that the value has &#xD; and &#xA; within the text. I understand those are the XML representation of carriage return and line feed.

When I import into BeautifulSoup, the value gets converted into the following:

<DIAttribute name="ObjectDesc" value="Line1
Line2
Line3"/>

You'll notice that the &#xd;&#xA; gets converted to a newline.

My use case requires that the value remains as the original. Any idea how to get that to stay? Or convert it back?

Source Code:

python: (2.7.11)

from bs4 import BeautifulSoup #version 4.4.0
s = BeautifulSoup(open('test.xml'),'lxml-xml',from_encoding="ansi")
print s.DIAttribute

#XML file looks like 
'''
<?xml version="1.0" encoding="UTF-8" ?>
<DIAttribute name="ObjectDesc" value="Line1&#xD;&#xA;Line2&#xD;&#xA;Line3"/>
'''

Notepad++ says the encoding of the source XML file is ANSI.

Things I've Tried:

  • I've scoured the documentation without any success.
  • Variations for line 3:

    print s.DIAttribute.prettify('ascii')
    print s.DIAttribute.prettify('windows-1252')
    print s.DIAttribute.prettify('ansi')
    print s.DIAttribute.prettify('utf-8')
    print s.DIAttribute['value'].replace('\r','&#xD;').replace('\n','&#xA;')  #This works, but it feels like a bandaid and will likely other problems will remain.
    

Any ideas anyone? I appreciate any comments/suggestions.

1 Answers
Related