本文整理汇总了Python中string.decode函数的典型用法代码示例。如果您正苦于以下问题:Python decode函数的具体用法?Python decode怎么用?Python decode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了decode函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:alihashmi01,项目名称:frame,代码行数:8,代码来源:grammarmodule.py
示例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
开发者ID:gmunkhbaatarmn,项目名称:natrix,代码行数:8,代码来源:natrix.py
示例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')
开发者ID:modalexii,项目名称:Small-WebApps,代码行数:9,代码来源:admin.py
示例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
开发者ID:csb60,项目名称:hyperspy,代码行数:10,代码来源:utils.py
示例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')
开发者ID:BIGGANI,项目名称:creativecommons.org,代码行数:11,代码来源:util.py
示例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')
开发者ID:MichaelBlume,项目名称:hg-git,代码行数:12,代码来源:git_handler.py
示例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]
开发者ID:anton-ryzhov,项目名称:pyicqt_auto_reconnect,代码行数:15,代码来源:utils.py
示例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'
开发者ID:cpence,项目名称:dotfiles,代码行数:7,代码来源:spcid666.py
示例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
开发者ID:adozenlines,项目名称:freevo1,代码行数:32,代码来源:misc.py
示例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')
开发者ID:chubbymaggie,项目名称:binwalk,代码行数:8,代码来源:compat.py
示例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)
开发者ID:jobevers,项目名称:duplicate_detector,代码行数:8,代码来源:util.py
示例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")
开发者ID:Brijen,项目名称:binwalk,代码行数:8,代码来源:compat.py
示例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')
开发者ID:dweinstein,项目名称:binwalk,代码行数:8,代码来源:compat.py
示例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
开发者ID:ifduyue,项目名称:buaatreeholes,代码行数:9,代码来源:lib.py
示例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)
开发者ID:pombredanne,项目名称:clldutils,代码行数:18,代码来源:misc.py
示例16: mapUTF8toXML
def mapUTF8toXML(string):
uString = string.decode('utf8')
string = ""
for uChar in uString:
i = ord(uChar)
if (i < 0x80) and (i > 0x1F):
string = string + chr(i)
else:
string = string + "&#x" + hex(i)[2:] + ";"
return string
开发者ID:kipulcha,项目名称:fonttools,代码行数:10,代码来源:M_E_T_A_.py
示例17: dewindows
def dewindows(string):
h = HTMLParser.HTMLParser()
try:
string = string.decode("windows-1252")
except:
try:
string = string.decode("windows-1251")
except:
try:
string = string.decode("ISO-8859-1")
except:
try:
string = string.decode("utf-8")
except:
pass
try:
string = h.unescape(string)
except:
pass
return string
开发者ID:CottageLabs,项目名称:leaps,代码行数:20,代码来源:util.py
示例18: to_unicode
def to_unicode(string):
"""
Converts a 'string' to unicode
"""
if not isinstance(string, unicode):
if not isinstance(string,str):
raise TypeError('You are required to pass either unicode or string here, not: %r (%s)' % (type(string), string))
try:
string = string.decode("UTF-8")
except UnicodeDecodeError, exc:
raise TypeError(str(exc))
开发者ID:noQ,项目名称:TriviaOnline,代码行数:11,代码来源:helpers.py
示例19: assertContained
def assertContained(self, values, string, additional_info=''):
if type(values) not in [list, tuple]: values = [values]
for value in values:
if type(value) is unicode: string = string.decode('UTF-8') # If we have any non-ASCII chars in the expected string, treat the test string from ASCII as UTF8 as well.
if type(string) is not str and type(string) is not unicode: string = string()
if value in string: return # success
raise Exception("Expected to find '%s' in '%s', diff:\n\n%s\n%s" % (
limit_size(values[0]), limit_size(string),
limit_size(''.join([a.rstrip()+'\n' for a in difflib.unified_diff(values[0].split('\n'), string.split('\n'), fromfile='expected', tofile='actual')])),
additional_info
))
开发者ID:Unity-Technologies,项目名称:emscripten,代码行数:11,代码来源:runner.py
示例20: fsdecode
def fsdecode(string):
u"""Decode byte strings to unicode with file system encoding.
This function is modelled after its namesake in the Python 3 os.path module.
"""
if isinstance(string, str):
return string.decode( sys.getfilesystemencoding() )
elif isinstance(string, unicode):
return string
else:
raise TypeError("argument is not of string type: {!r}".format(string))
开发者ID:gact,项目名称:gactutil,代码行数:11,代码来源:__init__.py
注:本文中的string.decode函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论