本文整理汇总了Python中chardet.__version__方法的典型用法代码示例。如果您正苦于以下问题:Python chardet.__version__方法的具体用法?Python chardet.__version__怎么用?Python chardet.__version__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chardet
的用法示例。
在下文中一共展示了chardet.__version__方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import chardet [as 别名]
# 或者: from chardet import __version__ [as 别名]
def main(argv=None):
'''
Handles command line arguments and gets things started.
:param argv: List of arguments, as if specified on the command-line.
If None, ``sys.argv[1:]`` is used instead.
:type argv: list of str
'''
# Get command line arguments
parser = argparse.ArgumentParser(
description="Takes one or more file paths and reports their detected \
encodings",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
conflict_handler='resolve')
parser.add_argument('input',
help='File whose encoding we would like to determine.',
type=argparse.FileType('rb'), nargs='*',
default=[sys.stdin])
parser.add_argument('--version', action='version',
version='%(prog)s {0}'.format(__version__))
args = parser.parse_args(argv)
for f in args.input:
if f.isatty():
print("You are running chardetect interactively. Press " +
"CTRL-D twice at the start of a blank line to signal the " +
"end of your input. If you want help, run chardetect " +
"--help\n", file=sys.stderr)
print(description_of(f, f.name))
示例2: main
# 需要导入模块: import chardet [as 别名]
# 或者: from chardet import __version__ [as 别名]
def main(argv=None):
"""
Handles command line arguments and gets things started.
:param argv: List of arguments, as if specified on the command-line.
If None, ``sys.argv[1:]`` is used instead.
:type argv: list of str
"""
# Get command line arguments
parser = argparse.ArgumentParser(
description="Takes one or more file paths and reports their detected \
encodings")
parser.add_argument('input',
help='File whose encoding we would like to determine. \
(default: stdin)',
type=argparse.FileType('rb'), nargs='*',
default=[sys.stdin if PY2 else sys.stdin.buffer])
parser.add_argument('--version', action='version',
version='%(prog)s {0}'.format(__version__))
args = parser.parse_args(argv)
for f in args.input:
if f.isatty():
print("You are running chardetect interactively. Press " +
"CTRL-D twice at the start of a blank line to signal the " +
"end of your input. If you want help, run chardetect " +
"--help\n", file=sys.stderr)
print(description_of(f, f.name))
示例3: test_guess_encoding_favor_utf_8
# 需要导入模块: import chardet [as 别名]
# 或者: from chardet import __version__ [as 别名]
def test_guess_encoding_favor_utf_8(self):
"""
Test that strings that could be UTF-8 or ISO-8859-* result in UTF-8.
python-chardet-3.0.4-2.fc27.noarch detects it as ISO-8859-9
python-chardet-2.2.1-1.el7_1.noarch detects it as ISO-8859-2
"""
data = "Šabata".encode("utf-8")
result = encoding_utils.guess_encoding(data)
chardet_result = chardet.detect(data)
self.assertEqual(result, "utf-8")
if chardet.__version__[0] == "3":
self.assertEqual(chardet_result["encoding"], "ISO-8859-9")
else:
self.assertEqual(chardet_result["encoding"], "ISO-8859-2")
示例4: detect_encodings
# 需要导入模块: import chardet [as 别名]
# 或者: from chardet import __version__ [as 别名]
def detect_encodings(data):
"""
Analyze the provided data for possible character encodings.
This simply wraps chardet and extracts all the potential encodings it
considered before deciding on a particular result.
:param data: An array of bytes to treat as text data
:type data: bytes
:return: A dictionary mapping possible encodings to confidence levels
:rtype: dict
"""
if not data:
# It's an empty string so we can safely say it's ascii
return {"ascii": 1.0}
# We can't use ``chardet.detect`` because we want to dig in the internals
# of the detector to bias the utf-8 result.
detector = universaldetector.UniversalDetector()
detector.reset()
detector.feed(data)
result = detector.close()
if not result:
return {"utf-8": 1.0}
encodings = {result["encoding"]: result["confidence"]}
if ch_version[0] == "3":
for prober in detector._charset_probers:
if hasattr(prober, "probers"):
for prober in prober.probers:
encodings[prober.charset_name] = prober.get_confidence()
else:
encodings[prober.charset_name] = prober.get_confidence()
else:
for prober in detector._mCharSetProbers:
if prober:
encodings[prober.get_charset_name()] = prober.get_confidence()
return encodings