I am not able to insert uni coded string to couchbase server


#1

I am using Python 2.7.9 with coucbhase 3

This is my code to insert to couchbase using Python SDK

from couchbase import Couchbase 
        connection = Couchbase.connect(bucket='default') #create connection to the couchbase database
        connection.set(fileName, dict(item)) #write the item to the couchbase database

Where fileName is a unique ID and item is an instance of class like this:
flexibleItem = FlexibleItem()
flexibleItem['location'] = "UAE \u202a>\u202a Dubai \u202a>\u202a The Palm Jumeirah ; 3.4 km from Dubai Media City \u00a0"

I got this exception:

couchbase.exceptions.ValueFormatError: <Couldn't encode value, inner_cause='ascii' codec can't decode byte 0xe2 in position 5: ordinal not in range(128),

though i already change the value to utf8 using this code:

value = value.encode(‘utf-8’, ‘ignore’)


#2

The default format for .set() is JSON, which means it is passed to the default JSON encoder; which expects either a Python unicode object, or a Python str convertible to unicode. Rather than doing value.encode(utf-8), you should be doing value.decode(utf-8).

Note that your example works as-is (The \uXXXX form doesn’t really seem to work unless the string itself is declared as u"\u202a"