FGDB - Error when writing unicode into blob field

834
3
Jump to solution
09-07-2018 05:25 AM
MaximeDemers
Occasional Contributor III

I got an SystemError : error return without exception set error when trying to write a unicode into a blob field into a table inside a FGDB using arcpy.da.UpdateCursor with ArcGIS v10.3

for instance:

import arcpy

value = u"a unicode string! with some accent éàèû"
with arcpy.da.UpdateCursor(r"c:\\my_fgdb.fgdb\my_table", ["blob_field"], where_clause="FID = '0') as cursor:
    for row in cursor:
        row[0] = value
        cursor.updateRow(row)

I know blob can store string, but is it normal it cannot store unicode?

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Since ArcGIS Desktop/ArcMap uses Python 2.x, inserting or updating a Blob field with a Python str object works because str is a sequence of bytes.    A Unicode string is a sequence of Unicode code points, not bytes.  Since a Blob field is designed to store binary data, you can't directly insert or update a Blob field with a Python Unicode string.  If you want to work with Unicode strings in Blob fields, you will need to encode and decode the strings first.

import arcpy

value = u"a unicode string! with some accent éàèû"
with arcpy.da.UpdateCursor(r"c:\\my_fgdb.fgdb\my_table", ["blob_field"], where_clause="FID = '0') as cursor:
    for row in cursor:
        row[0] = value.encode('utf8')
        cursor.updateRow(row)

View solution in original post

3 Replies
JoshuaBixby
MVP Esteemed Contributor

Since ArcGIS Desktop/ArcMap uses Python 2.x, inserting or updating a Blob field with a Python str object works because str is a sequence of bytes.    A Unicode string is a sequence of Unicode code points, not bytes.  Since a Blob field is designed to store binary data, you can't directly insert or update a Blob field with a Python Unicode string.  If you want to work with Unicode strings in Blob fields, you will need to encode and decode the strings first.

import arcpy

value = u"a unicode string! with some accent éàèû"
with arcpy.da.UpdateCursor(r"c:\\my_fgdb.fgdb\my_table", ["blob_field"], where_clause="FID = '0') as cursor:
    for row in cursor:
        row[0] = value.encode('utf8')
        cursor.updateRow(row)
MaximeDemers
Occasional Contributor III

Hello Joshua,

Thank you for the information about the blob field!

Do you have an idea why storing unicode in text field works?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The ArcGIS Text field is actually Unicode:  ArcGIS field data types—ArcGIS Help | ArcGIS Desktop 

The characters used for text vary by language. To allow text to more easily convert between languages, ArcGIS uses Unicode to encode characters.