Skip to content

Instantly share code, notes, and snippets.

@ken-itakura
Created May 25, 2017 07:29
Show Gist options
  • Save ken-itakura/3a6e460814a560dd06fc9e8e5e3128b1 to your computer and use it in GitHub Desktop.
Save ken-itakura/3a6e460814a560dd06fc9e8e5e3128b1 to your computer and use it in GitHub Desktop.
python unicode str conversion functions
# -*- coding: utf-8 -*-
"""
Created on 2017/5/25
@author: itakura
"Unicode <-> str 変換関連"
"""
class strutils:
@staticmethod
def force_to_str(src, none_to_empty = True):
"""
str, unicode, None, List, tuple, object などをとにかく str にする
:param src: もとの Object
:param none_to_empty: True の場合、None は空文字に変換, False の場合 "None"
:return: str
"""
if src is None:
if none_to_empty:
return ""
else:
return "None"
if isinstance(src, str):
return src
if isinstance(src, unicode):
return src.encode('utf-8')
if isinstance(src, tuple):
strlist = [strutils.force_to_str(s, none_to_empty) for s in src]
return "(" + ",".join(strlist) + ")"
if isinstance(src, list):
strlist = [strutils.force_to_str(s, none_to_empty) for s in src]
return "[" + ",".join(strlist) + "]"
## just try to apply str() for any other object
try:
return(str(src))
except Exception, e:
return str(e)
@staticmethod
def force_to_unicode(src, none_to_empty = True):
"""
str, unicode, None, List, tuple, object などをとにかく unicode にする
:param src: もとの Object
:param none_to_empty: True の場合、None は空文字に変換, False の場合 "None"
:return: unicode。src が List や tuple だった場合、unicode の List
"""
if src is None:
if none_to_empty:
return u""
else:
return u"None"
if isinstance(src, str):
return unicode(src, 'utf-8')
if isinstance(src, unicode):
return src
if isinstance(src, tuple):
unicodelist = [strutils.force_to_unicode(s, none_to_empty) for s in src]
return u"(" + u",".join(unicodelist) + u")"
if isinstance(src, list):
unicodelist = [strutils.force_to_unicode(s, none_to_empty) for s in src]
return u"[" + u",".join(unicodelist) + u"]"
## just try to apply str() for any other object
try:
return(unicode(str(src), 'utf-8'))
except Exception, e:
return unicode(str(e), 'utf-8')
@staticmethod
def force_to_str_list(srclist, none_to_empty = True, flatten = False):
"""
List, tuple に含まれる内容を str にして list にする
:param src: もとの Object
:param none_to_empty: True の場合、None は空文字に変換, False の場合 "None"
:param flatten: True の場合 flat にする
:return: str
"""
result = []
for src in srclist:
if isinstance(src, tuple) or isinstance(src, list):
rlist = strutils.force_to_str_list(src, none_to_empty, flatten)
if flatten:
result.extend(rlist)
else:
result.append(rlist)
else:
result.append(strutils.force_to_str(src, none_to_empty))
return result
@staticmethod
def force_to_unicode_list(srclist, none_to_empty = True, flatten = False):
"""
List, tuple に含まれる内容を unicode にして list にする
:param src: もとの Object
:param none_to_empty: True の場合、None は空文字に変換, False の場合 "None"
:param flatten: True の場合 flat にする
:return: str
"""
result = []
for src in srclist:
if isinstance(src, tuple) or isinstance(src, list):
rlist = strutils.force_to_unicode_list(src, none_to_empty, flatten)
if flatten:
result.extend(rlist)
else:
result.append(rlist)
else:
result.append(strutils.force_to_unicode(src, none_to_empty))
return result
@staticmethod
def force_print(src, none_to_empty = True):
"""
str, unicode, None, List, tuple, object などをとにかく 文字列にして print する
:param src:
:param none_to_empty:
:return:
"""
print strutils.force_to_str(src, none_to_empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment