本文整理汇总了Python中future.builtins.str.encode方法的典型用法代码示例。如果您正苦于以下问题:Python str.encode方法的具体用法?Python str.encode怎么用?Python str.encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类future.builtins.str
的用法示例。
在下文中一共展示了str.encode方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _encode_result
# 需要导入模块: from future.builtins import str [as 别名]
# 或者: from future.builtins.str import encode [as 别名]
def _encode_result(obj, encoding=_implicit_encoding,
errors=_implicit_errors):
return obj.encode(encoding, errors)
示例2: encode
# 需要导入模块: from future.builtins import str [as 别名]
# 或者: from future.builtins.str import encode [as 别名]
def encode(self, encoding='ascii', errors='strict'):
return self._encoded_counterpart(*(x.encode(encoding, errors) for x in self))
示例3: geturl
# 需要导入模块: from future.builtins import str [as 别名]
# 或者: from future.builtins.str import encode [as 别名]
def geturl(self):
return urlunparse(self)
# Set up the encode/decode result pairs
示例4: unquote_to_bytes
# 需要导入模块: from future.builtins import str [as 别名]
# 或者: from future.builtins.str import encode [as 别名]
def unquote_to_bytes(string):
"""unquote_to_bytes('abc%20def') -> b'abc def'."""
# Note: strings are encoded as UTF-8. This is only an issue if it contains
# unescaped non-ASCII characters, which URIs should not.
if not string:
# Is it a string-like object?
string.split
return bytes(b'')
if isinstance(string, str):
string = string.encode('utf-8')
### For Python-Future:
# It is already a byte-string object, but force it to be newbytes here on
# Py2:
string = bytes(string)
###
bits = string.split(b'%')
if len(bits) == 1:
return string
res = [bits[0]]
append = res.append
for item in bits[1:]:
try:
append(_hextobyte[item[:2]])
append(item[2:])
except KeyError:
append(b'%')
append(item)
return bytes(b'').join(res)
示例5: to_bytes
# 需要导入模块: from future.builtins import str [as 别名]
# 或者: from future.builtins.str import encode [as 别名]
def to_bytes(url):
"""to_bytes(u"URL") --> 'URL'."""
# Most URL schemes require ASCII. If that changes, the conversion
# can be relaxed.
# XXX get rid of to_bytes()
if isinstance(url, str):
try:
url = url.encode("ASCII").decode()
except UnicodeError:
raise UnicodeError("URL " + repr(url) +
" contains non-ASCII characters")
return url
示例6: quote_from_bytes
# 需要导入模块: from future.builtins import str [as 别名]
# 或者: from future.builtins.str import encode [as 别名]
def quote_from_bytes(bs, safe='/'):
"""Like quote(), but accepts a bytes object rather than a str, and does
not perform string-to-bytes encoding. It always returns an ASCII string.
quote_from_bytes(b'abc def\x3f') -> 'abc%20def%3f'
"""
if not isinstance(bs, (bytes, bytearray)):
raise TypeError("quote_from_bytes() expected bytes")
if not bs:
return str('')
### For Python-Future:
bs = bytes(bs)
###
if isinstance(safe, str):
# Normalize 'safe' by converting to bytes and removing non-ASCII chars
safe = str(safe).encode('ascii', 'ignore')
else:
### For Python-Future:
safe = bytes(safe)
###
safe = bytes([c for c in safe if c < 128])
if not bs.rstrip(_ALWAYS_SAFE_BYTES + safe):
return bs.decode()
try:
quoter = _safe_quoters[safe]
except KeyError:
_safe_quoters[safe] = quoter = Quoter(safe).__getitem__
return str('').join([quoter(char) for char in bs])