File: encoding.py

package info (click to toggle)
python-clevercsv 0.8.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,076 kB
  • sloc: python: 6,184; ansic: 870; makefile: 90
file content (64 lines) | stat: -rw-r--r-- 1,497 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-

"""Functionality to detect file encodings

Author: G.J.J. van den Burg
License: See the LICENSE file

This file is part of CleverCSV.

"""

from typing import Optional

import chardet

from ._optional import import_optional_dependency
from ._types import _OpenFile


def get_encoding(
    filename: _OpenFile, try_cchardet: bool = True
) -> Optional[str]:
    """Get the encoding of the file

    This function uses the chardet package for detecting the encoding of a
    file.

    Parameters
    ----------
    filename: str
        Path to a file

    try_cchardet: bool
        Whether to run detection using cChardet if it is available. This can be
        faster, but may give different results than using chardet.

    Returns
    -------
    encoding: str
        Encoding of the file.
    """
    if try_cchardet:
        cchardet = import_optional_dependency(
            "cchardet", raise_on_missing=False
        )
    else:
        cchardet = None

    if cchardet is None:
        detector = chardet.UniversalDetector()
    else:
        detector = cchardet.UniversalDetector()

    final_chunk = False
    blk_size = 65536
    with open(filename, "rb") as fid:
        while (not final_chunk) and (not detector.done):
            chunk = fid.read(blk_size)
            if len(chunk) < blk_size:
                final_chunk = True
            detector.feed(chunk)
    detector.close()
    encoding = detector.result.get("encoding", None)
    return encoding