当前位置: 首页>>代码示例>>Python>>正文


Python chardet.__version__方法代码示例

本文整理汇总了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)) 
开发者ID:war-and-code,项目名称:jawfish,代码行数:31,代码来源:chardetect.py

示例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)) 
开发者ID:danielecook,项目名称:gist-alfred,代码行数:30,代码来源:chardetect.py

示例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") 
开发者ID:Pagure,项目名称:pagure,代码行数:17,代码来源:test_pagure_lib_encoding_utils.py

示例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 
开发者ID:Pagure,项目名称:pagure,代码行数:41,代码来源:encoding_utils.py


注:本文中的chardet.__version__方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。