def safestr(obj, encoding='utf-8'):
 if isinstance(obj, unicode):
  return obj.encode(encoding)
 elif isinstance(obj, str):
  return obj
 elif hasattr(obj, 'next'):
  return itertools.imap(safestr, obj)
 else:
  return str(obj)

def safestr ( obj , encoding = 'utf-8' ) : if isinstance ( obj , unicode ) : return obj . encode ( encoding ) elif isinstance ( obj , str ) : return obj elif hasattr ( obj , 'next' ) : return itertools . imap ( safestr , obj ) else : return str ( obj )
converts any given object to utf-8 encoded string .