sunninghao_change

sunninghao_branch
liguanwei 2 months ago
parent 8b01ccc517
commit 87f1fc758c

@ -15,7 +15,6 @@
# 02110-1301 USA
######################### END LICENSE BLOCK #########################
from .compat import PY2, PY3
from .universaldetector import UniversalDetector
from .version import __version__, VERSION
@ -25,15 +24,28 @@ def detect(byte_str):
"""
Detect the encoding of the given byte string.
This function uses the UniversalDetector class to determine the encoding
of a given byte string. It creates a new UniversalDetector instance,
feeds the byte string to it, and then returns the detected encoding.
:param byte_str: The byte sequence to examine.
:type byte_str: ``bytes`` or ``bytearray``
:return: The detected encoding.
"""
# Check if the input is of the correct type
if not isinstance(byte_str, bytearray):
if not isinstance(byte_str, bytes):
raise TypeError('Expected object of type bytes or bytearray, got: '
'{0}'.format(type(byte_str)))
else:
# If the input is of type bytes, convert it to bytearray
byte_str = bytearray(byte_str)
# Create a new UniversalDetector instance
detector = UniversalDetector()
# Feed the byte string to the detector
detector.feed(byte_str)
return detector.close()
# Close the detector and return the detected encoding
return detector.close()
Loading…
Cancel
Save