Python non-ascii characters

1167
1
09-28-2011 10:42 AM
JamesDewar
New Contributor
I have a python file that creates and populates a table in ms sql (via modelbuilder).  The only sticking point is that the code breaks if there are any non-ascii characters or single apostrophes (and there are quite a few of each).  Although I can run the replace function to rid the strings of apostrophes, I would prefer to keep it intact. I have also tried converting the data into utf-8, but no luck there either.  The python code is attached as a text file. 

An example of the error messages are "'ascii' codec can't encode character u'\2013' in position..." (for non-ascii characters) and "class 'pyodbc.ProgrammingError'>: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server] Incorrect syntax near 'S, 230 X 90M.; Eligibilty..." (for the single quotes). The python code is included below. I believe the point in the code where this break occurs is after the following line: InsertValue = str(row.GetValue(CurrentField['Name'])).

Any help would be greatly appreciated.

James
Tags (2)
0 Kudos
1 Reply
MikeWynne
New Contributor II
This should remove the non-asciis:
def removeNonAscii(s): return "".join(filter(lambda x: ord(x)<128, s))

Then, something like this in your code should work:
InsertValue = removeNonAscii(str(row.GetValue(CurrentField['Name'])))

Mike
0 Kudos