本文整理汇总了Python中string.decode函数的典型用法代码示例。如果您正苦于以下问题:Python decode函数的具体用法?Python decode怎么用?Python decode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了decode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encode
def encode(self, string):
clean_sentence_unwantedchars= '["\t\n ]+'
string = string.encode('utf8')
string = string.decode('utf-8')
string = re.sub(clean_sentence_unwantedchars, ' ', string)
string = string.encode('ascii', 'replace').encode('utf-8')
string = string.decode('utf-8')
return str(string)
示例2: ensure_unicode
def ensure_unicode(string):
if isinstance(string, str):
try:
string = string.decode("utf-8")
except UnicodeDecodeError:
string = string.decode("unicode-escape")
return string
示例3: scrubstring
def scrubstring(string):
from scrubber import Scrubber
scrubber = Scrubber(autolink=True)
try:
string = string.decode('ascii')
except UnicodeDecodeError:
string = string.decode('utf-8')
string = scrubber.scrub(string)
return string.encode('utf-8')
示例4: ensure_unicode
def ensure_unicode(stuff, encoding = 'utf8', encoding2 = 'latin-1'):
if type(stuff) is not str and type(stuff) is not np.string_:
return stuff
else:
string = stuff
try:
string = string.decode(encoding)
except:
string = string.decode(encoding2, errors = 'ignore')
return string
示例5: unicode_cleaner
def unicode_cleaner(string):
if isinstance(string, unicode):
return string
try:
return string.decode('utf-8')
except UnicodeError:
try:
return string.decode('latin-1')
except UnicodeError:
return string.decode('utf-8', 'ignore')
示例6: decode_guess
def decode_guess(self, string, encoding):
# text is not valid utf-8, try to make sense of it
if encoding:
try:
return string.decode(encoding).encode('utf-8')
except UnicodeDecodeError:
pass
try:
return string.decode('latin-1').encode('utf-8')
except UnicodeDecodeError:
return string.decode('ascii', 'replace').encode('utf-8')
示例7: tryDecode
def tryDecode(string):
try:
string_d = string.decode('utf-8','strict')
enc = 'utf-8'
except:
try:
string_d = string.decode(config.encoding,'strict')
enc = config.encoding
except:
enc = 'iso-8859-1'
try:
string_d = string.decode('iso-8859-1','strict')
except:
string_d = string.decode('iso-8859-1','replace')
return [string_d, enc]
示例8: _decode_string
def _decode_string(self, string):
for encoding in ['ascii', 'UTF8', 'latin-1']:
try:
return string.decode(encoding)
except:
pass
return 'INVALID ENCODING'
示例9: htmlenties2txt
def htmlenties2txt(string, encoding="latin-1"):
"""
Resolves all the HTML entities in the input string.
Returns a Unicode string with the entities resolved.
"""
try:
string = string.decode(encoding)
except:
pass
i = 0
while i < len(string):
amp = string.find("&", i) # find & as start of entity
if amp == -1: # not found
break
i = amp + 1
semicolon = string.find(";", amp) # find ; as end of entity
if string[amp + 1] == "#": # numerical entity like "'"
entity = string[amp:semicolon+1]
replacement = unichr(int(entity[2:-1]))
else:
entity = string[amp:semicolon + 1]
if semicolon - amp > 7:
continue
try:
# the array has mappings like "Uuml" -> "�"
replacement = unichr(htmlentitydefs.name2codepoint[entity[1:-1]])
except KeyError:
continue
string = string.replace(entity, replacement)
return string
示例10: string_decode
def string_decode(string):
'''
For cross compatibility between Python 2 and Python 3 strings.
'''
if PY_MAJOR_VERSION > 2:
return bytes(string, 'utf-8').decode('unicode_escape')
else:
return string.decode('string_escape')
示例11: tryDecode
def tryDecode(string):
codec = ('ascii', 'latin-1', 'utf-8')
for c in codec:
try:
return string.decode(c)
except UnicodeDecodeError:
continue
raise UnicodeDecodeError(string)
示例12: string_decode
def string_decode(string):
"""
For cross compatibility between Python 2 and Python 3 strings.
"""
if PY_MAJOR_VERSION > 2:
return bytes(string, "utf-8").decode("unicode_escape")
else:
return string.decode("string_escape")
示例13: string_decode
def string_decode(string):
'''
For cross compatibility between Python 2 and Python 3 strings.
'''
if sys.version_info.major > 2:
return bytes(string, 'utf-8').decode('unicode_escape')
else:
return string.decode('string_escape')
示例14: mb_code
def mb_code(string, coding="utf-8"):
if isinstance(string, unicode):
return string.encode(coding)
for c in ('utf-8', 'gb2312', 'gbk', 'gb18030', 'big5'):
try:
return string.decode(c).encode(coding)
except:
pass
return string
示例15: encoded
def encoded(string, encoding='utf8'):
"""Cast string to binary_type.
:param string: six.binary_type or six.text_type
:param encoding: encoding which the object is forced to
:return: six.binary_type
"""
assert isinstance(string, string_types) or isinstance(string, binary_type)
if isinstance(string, text_type):
return string.encode(encoding)
try:
# make sure the string can be decoded in the specified encoding ...
string.decode(encoding)
return string
except UnicodeDecodeError:
# ... if not use latin1 as best guess to decode the string before encoding as
# specified.
return string.decode('latin1').encode(encoding)