regex to comment out special characters

This is the place for queries that don't fit in any of the other categories.

regex to comment out special characters

Postby dogdaynoon » Thu Jul 04, 2013 12:00 am

I have multiple xml files that contain the following types of text and am trying to insert all of it into an MSSQL Table.
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<metadata xml:lang="en">
&nbsp; &quot;
 xmlns:xs='http://www.w3.org/2001/XMLSchema'

As you can see there are html special characters and others in this text and they are reaping havoc on my sql insert statements.
How would a person go about searching for these characters and adding a "\" in front of them to perpare this text for MSSQL inserstion?

I can replace all of it with something such as this:
Code: Select all
rgX = re.compile('\&[a-zA-z]+\;)
changed = rgX.sub("\\\\", fileName)


but i would really like to see them commented out... such as &quot; becomes \&quot\;

I guess another question would be, would that fix the problem inserting it into MSSQL?
I know you don't exactly know what that problem is but trust me, it comes from characters such as these ;, &, ", ' ,= and so forth...
btw i am using pyodbc to insert text to MSSQL 2012 running python 3.3 under Windows 7 64 bit SP1
Thanks!
James
dogdaynoon
 
Posts: 12
Joined: Thu May 23, 2013 11:56 pm

Re: regex to comment out special characters

Postby metulburr » Thu Jul 04, 2013 12:46 am

but i would really like to see them commented out... such as &quot; becomes \&quot\;

Why use regexp when str.replace() exists?
Code: Select all
text = '''
<?xml version="1.0" encoding="UTF-8"?>
<metadata xml:lang="en">
&nbsp; &quot;
 xmlns:xs='http://www.w3.org/2001/XMLSchema'
'''

print(text.replace('&quot;', '\&quot\;'))
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests