本文整理汇总了Python中codecs.latin_1_encode方法的典型用法代码示例。如果您正苦于以下问题:Python codecs.latin_1_encode方法的具体用法?Python codecs.latin_1_encode怎么用?Python codecs.latin_1_encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类codecs
的用法示例。
在下文中一共展示了codecs.latin_1_encode方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _to_bytes
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def _to_bytes(msg: Union[str, bytes]) -> bytes:
"""
Provides the ASCII bytes for the given string. This is purely to provide
python 3 compatability, normalizing the unicode/ASCII change in the version
bump. For an explanation of this see...
http://python3porting.com/problems.html#nicer-solutions
:param msg: string to be converted
:returns: ASCII bytes for string
"""
if isinstance(msg, str):
return codecs.latin_1_encode(msg, 'replace')[0] # type: ignore
else:
return msg
示例2: b
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def b(s):
return codecs.latin_1_encode(s)[0]
示例3: b
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def b(s):
# BSON and socket operations deal in binary data. In
# python 3 that means instances of `bytes`. In python
# 2.6 and 2.7 you can create an alias for `bytes` using
# the b prefix (e.g. b'foo'). Python 2.4 and 2.5 don't
# provide this marker so we provide this compat function.
# In python 3.x b('foo') results in b'foo'.
# See http://python3porting.com/problems.html#nicer-solutions
return codecs.latin_1_encode(s)[0]
示例4: _b
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def _b(x):
return codecs.latin_1_encode(x)[0]
示例5: encode
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def encode(self, input, final=False):
return codecs.latin_1_encode(input,self.errors)[0]
示例6: test_latin_1_encode
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def test_latin_1_encode(self):
#sanity
new_str, size = codecs.latin_1_encode("abc")
self.assertEqual(new_str, 'abc')
self.assertEqual(size, 3)
# so many ways to express latin 1...
for x in ['iso-8859-1', 'iso8859-1', '8859', 'cp819', 'latin', 'latin1', 'L1']:
self.assertEqual('abc'.encode(x), 'abc')
#TODO: @skip("multiple_execute")
示例7: b
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def b(s):
# BSON and socket operations deal in binary data. In
# python 3 that means instances of `bytes`. In python
# 2.6 and 2.7 you can create an alias for `bytes` using
# the b prefix (e.g. b'foo').
# See http://python3porting.com/problems.html#nicer-solutions
return codecs.latin_1_encode(s)[0]
示例8: b
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def b(x, encoder=None):
"""Converts `x` to a bytes string if not already.
:param x: The string.
:param encoder: Optional codec encoder to perform the conversion. The default is
`codecs.latin_1_encode`.
:return: The byte string if conversion was needed.
"""
if isinstance(x, bytes):
return x
else:
import codecs
encoder = encoder or codecs.latin_1_encode
return encoder(x)[0]
示例9: b
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def b(x):
if isinstance(x, six.binary_type):
return x
else:
return codecs.latin_1_encode(x)[0]
示例10: b
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def b(x):
if isinstance(x, bytes) is False:
return codecs.latin_1_encode(x)[0]
return x
示例11: makebyte
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def makebyte(x):
return codecs.latin_1_encode(x)[0]
示例12: b
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def b(s):
# BSON and socket operations deal in binary data. In
# python 3 that means instances of `bytes`. In python
# 2.7 you can create an alias for `bytes` using
# the b prefix (e.g. b'foo').
# See http://python3porting.com/problems.html#nicer-solutions
return codecs.latin_1_encode(s)[0]
示例13: test_latin_1_encode
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import latin_1_encode [as 别名]
def test_latin_1_encode(self):
#sanity
new_str, num_processed = codecs.latin_1_encode("abc")
self.assertEqual(new_str, b'abc')
self.assertEqual(num_processed, 3)
# so many ways to express latin 1...
for x in ['iso-8859-1', 'iso8859-1', '8859', 'cp819', 'latin', 'latin1', 'L1']:
self.assertEqual('abc'.encode(x), b'abc')
self.assertRaises(TypeError, codecs.latin_1_encode, b"abc")
self.assertRaises(TypeError, codecs.latin_1_encode, None)
self.assertRaises(TypeError, codecs.latin_1_encode, None, None)
self.assertRaises(UnicodeEncodeError, codecs.latin_1_encode, "\u0100", None)