本文整理汇总了Python中string.decode方法的典型用法代码示例。如果您正苦于以下问题:Python string.decode方法的具体用法?Python string.decode怎么用?Python string.decode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类string
的用法示例。
在下文中一共展示了string.decode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_noun_phrases
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def find_noun_phrases(string):
noun_counts = {}
try:
blob = TextBlob(string.decode('utf-8'))
except:
print "Error occured"
return None
if blob.detect_language() != "en":
print "Tweets are not in English"
sys.exit(1)
else:
for noun in blob.noun_phrases:
if noun in stopwords.words('english') or noun in extra_stopwords or noun == '' or len(noun) < 3:
pass
else:
noun_counts[noun.lower()] = blob.words.count(noun)
sorted_noun_counts = sorted(noun_counts.items(), key=operator.itemgetter(1),reverse=True)
return sorted_noun_counts[0:15]
示例2: encode
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def encode(string):
return unicodedata.normalize('NFKD', string.decode('utf-8')).encode('ascii', 'ignore')
示例3: parse_unicode_str
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def parse_unicode_str(string):
try:
return string.decode('utf8')
except (UnicodeEncodeError, UnicodeDecodeError):
return string
示例4: string_to_key
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def string_to_key(cls, string, salt, params):
utf16string = string.decode('UTF-8').encode('UTF-16LE')
return Key(cls.enctype, MD4.new(utf16string).digest())
示例5: string_to_key
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def string_to_key(cls, string, salt, params):
utf16string = string.decode('UTF-8').encode('UTF-16LE')
return Key(cls.enctype, hashlib.new('md4', utf16string).digest())
示例6: parse_unicode_str
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def parse_unicode_str(string):
try:
return string.decode('utf8')
except UnicodeEncodeError:
return string
示例7: byte_xor
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def byte_xor(stream: bytes, key: int) -> str:
return bytes(byte ^ key for byte in stream).decode(errors="ignore")
示例8: decode_save
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def decode_save(cls, save: Union[bytes, str], needs_xor: bool = True) -> str:
if isinstance(save, str):
save = save.encode()
if needs_xor:
save = cls.byte_xor(save, 11)
save += "=" * (4 - len(save) % 4)
return inflate(urlsafe_b64decode(save.encode())).decode(errors="ignore")
示例9: decode_mac_save
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def decode_mac_save(cls, save: Union[bytes, str], *args, **kwargs) -> str:
if isinstance(save, str):
save = save.encode()
data = cls.cipher.decrypt(save)
last = data[-1]
if last < 16:
data = data[:-last]
return data.decode(errors="ignore")
示例10: encode_save
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def encode_save(cls, save: Union[bytes, str], needs_xor: bool = True) -> str:
if isinstance(save, str):
save = save.encode()
final = urlsafe_b64encode(deflate(save))
if needs_xor:
final = cls.byte_xor(final, 11)
else:
final = final.decode()
return final
示例11: do_base64
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def do_base64(
cls, data: str, encode: bool = True, errors: str = "strict", safe: bool = True
) -> str:
try:
if encode:
return urlsafe_b64encode(data.encode(errors=errors)).decode(errors=errors)
else:
padded = data + ("=" * (4 - len(data) % 4))
return urlsafe_b64decode(padded.encode(errors=errors)).decode(errors=errors)
except Exception:
if safe:
return data
raise
示例12: unzip
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def unzip(cls, string: Union[bytes, str]) -> Union[bytes, str]:
"""Decompresses a level string.
Used to unzip level data.
Parameters
----------
string: Union[:class:`bytes`, :class:`str`]
String to unzip, encoded in Base64.
Returns
-------
Union[:class:`bytes`, :class:`str`]
Unzipped level data, as a stream.
"""
if isinstance(string, str):
string = string.encode()
unzipped = inflate(urlsafe_b64decode(string))
try:
final = unzipped.decode()
except UnicodeDecodeError:
final = unzipped
return final
示例13: zip
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def zip(cls, string: Union[bytes, str], append_semicolon: bool = True) -> str:
if isinstance(string, bytes):
string = string.decode(errors="ignore")
if append_semicolon and not string.endswith(";"):
string += ";"
return cls.encode_save(string, needs_xor=False)
示例14: mapUTF8toXML
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
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
示例15: _floatconstants
# 需要导入模块: import string [as 别名]
# 或者: from string import decode [as 别名]
def _floatconstants():
_BYTES = '7FF80000000000007FF0000000000000'.decode('hex')
if sys.byteorder != 'big':
_BYTES = _BYTES[:8][::-1] + _BYTES[8:][::-1]
nan, inf = struct.unpack('dd', _BYTES)
return nan, inf, -inf