Skip to content Skip to sidebar Skip to footer

Removing Spaces And Non-printable Character In Python

I am working with xml file using lxml etree xpath method. My code is from lxml import etree File='c:\file.xml' doc=etree.parse(File) alltext = doc.xpath('descendant-or-self::text()

Solution 1:

If you don't mind to do it using regex:

import re
clump = re.sub(r'[\n\t]+', ' ', clump)

If you want to put any other characters to remove, just place those inside the []

Solution 2:

You can try this:

''.join(clump.split())

Hope, that will solve the problem! To improve this, you can use re and I am using Sabuj's code:

>>>import re>>>re.sub(r'[\n\t]+', ' ', clump.strip())

Post a Comment for "Removing Spaces And Non-printable Character In Python"