I wrote a function which converts the unicode encoding in my input data to utf-8 encoding.
The function is capable to handle the raw string OR dict OR list as an input and returns the respective utf-8 encoded output.
This function is a part of bigger project that i am working on. This function is giving correct output as expected.
The problem is that it is becoming a bottleneck for me in terms of execution time. Though the current execution time is turning out to be close to ~1ms but as i said, its a part of bigger project where i had to call this function repetitively which ultimately is harming my response time of API
def fix_unicode(self, data):
if isinstance(data, unicode):
return data.encode('utf-8')
elif isinstance(data, dict):
data = dict((fix_unicode(k), fix_unicode(data[k])) for k in data)
elif isinstance(data, list):
for i in xrange(0, len(data)):
data[i] = fix_unicode(data[i])
return data
Can i further optimise this function ? if yes how ?