### Install CleverCSV (Full Version) Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/index.md Install the full version of CleverCSV, including the command line interface and all optional dependencies. ```bash $ pip install clevercsv[full] ``` -------------------------------- ### Install CleverCSV (Core Version) Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Install a lighter, core version of CleverCSV using pip. ```bash $ pip install clevercsv ``` -------------------------------- ### Install CleverCSV with Full Features Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/index.md Install the full version of CleverCSV using pip to include the command-line interface. ```bash pip install clevercsv[full] ``` -------------------------------- ### Install CleverCSV (Full Version) Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Install the full version of CleverCSV, including the command line interface and all optional dependencies, using pip. ```bash $ pip install clevercsv[full] ``` -------------------------------- ### Install CleverCSV with Full Features Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Install the full version of CleverCSV using pip, which includes the command-line interface. This command is necessary to use `clevercsv` commands in your terminal. ```bash # Install the full version of CleverCSV (this includes the command line interface) $ pip install clevercsv[full] ``` -------------------------------- ### Explore CSV Data Interactively Source: https://github.com/alan-turing-institute/clevercsv/blob/master/README.md Start an interactive Python shell with CSV data loaded. The file can be loaded as a list of lists or a Pandas DataFrame using the -p flag. ```bash $ clevercsv explore milk.csv Dropping you into an interactive shell. CleverCSV has loaded the data into the variable: rows >>> >>> len(rows) 381 ``` ```bash $ clevercsv explore -p imdb.csv Dropping you into an interactive shell. CleverCSV has loaded the data into the variable: df >>> >>> df.head() fn tid ... War Western 0 titles01/tt0012349 tt0012349 ... 0 0 1 titles01/tt0015864 tt0015864 ... 0 0 2 titles01/tt0017136 tt0017136 ... 0 0 3 titles01/tt0017925 tt0017925 ... 0 0 4 titles01/tt0021749 tt0021749 ... 0 0 [5 rows x 44 columns] ``` -------------------------------- ### Import CleverCSV as a CSV Module Replacement Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Import CleverCSV to use it as a direct replacement for Python's built-in csv module. No additional setup is required. ```python import clevercsv ``` -------------------------------- ### Speed Up Dialect Detection with a Sample Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md For large files, provide a sample of the document to clevercsv.Sniffer().sniff() to speed up dialect detection. This reads only the specified number of characters. ```python dialect = clevercsv.Sniffer().sniff(fp.read(10000)) ``` -------------------------------- ### clevercsv.field_size_limit Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Gets or sets the maximum field size limit for CSV parsing. This is a global setting. ```APIDOC ## field_size_limit() ### Description Gets or sets the maximum field size limit for CSV parsing. ### Method (Not specified, likely a function call) ### Endpoint (Not applicable, this is a Python function) ### Parameters (Parameters not explicitly detailed in the source) ### Request Example (Not applicable) ### Response (Response details not explicitly detailed in the source) ``` -------------------------------- ### clevercsv.cparser_util.field_size_limit Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Get or set the limit for the field size in CSV parsing. This function is adapted from the Python CSV module. ```APIDOC ## field_size_limit(*args: Any, **kwargs: Any) -> int ### Description Get/Set the limit to the field size. ### Parameters * **args** (Any) - Positional arguments. * **kwargs** (Any) - Keyword arguments. ### Returns **int** - The current field size limit. ``` -------------------------------- ### Explore CSV File as DataFrame with CleverCSV CLI Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Use the `clevercsv explore` command to open an interactive Python shell with the CSV file loaded as a Pandas DataFrame. This allows for immediate data exploration. ```bash # Explore the CSV file as a Pandas dataframe $ clevercsv explore -p imdb.csv ``` -------------------------------- ### clevercsv.consistency Module Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Tools for detecting and scoring the consistency of CSV dialects. ```APIDOC ## ConsistencyDetector.compute_consistency_scores() ### Description Computes consistency scores for different CSV dialects. ### Method `clevercsv.consistency.ConsistencyDetector.compute_consistency_scores(data, dialect)` ### Parameters * **data** (iterable) - The CSV data to analyze. * **dialect** (object) - The CSV dialect to score. ### Returns * (dict) - A dictionary of consistency scores. ``` ```APIDOC ## ConsistencyDetector.compute_type_score() ### Description Computes a score based on the type consistency of fields within a CSV. ### Method `clevercsv.consistency.ConsistencyDetector.compute_type_score(data, dialect)` ### Parameters * **data** (iterable) - The CSV data to analyze. * **dialect** (object) - The CSV dialect to score. ### Returns * (float) - The type consistency score. ``` ```APIDOC ## ConsistencyDetector.detect() ### Description Detects the most consistent CSV dialect for the given data. ### Method `clevercsv.consistency.ConsistencyDetector.detect(data)` ### Parameters * **data** (iterable) - The CSV data to analyze. ### Returns * (object) - The most consistent CSV dialect. ``` ```APIDOC ## ConsistencyDetector.get_best_dialects() ### Description Retrieves a list of the best-fitting CSV dialects for the data, ordered by consistency. ### Method `clevercsv.consistency.ConsistencyDetector.get_best_dialects(data, n=5)` ### Parameters * **data** (iterable) - The CSV data to analyze. * **n** (int, optional) - The maximum number of dialects to return. Defaults to 5. ### Returns * (list) - A list of the top N most consistent CSV dialects. ``` ```APIDOC ## detect_dialect_consistency() ### Description Detects the consistency of a given CSV dialect against the provided data. ### Method `clevercsv.consistency.detect_dialect_consistency(data, dialect)` ### Parameters * **data** (iterable) - The CSV data to analyze. * **dialect** (object) - The CSV dialect to check. ### Returns * (float) - The consistency score of the dialect. ``` -------------------------------- ### Explore CSV Data in Interactive Shell (List of Lists) Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Explore a CSV file by launching an interactive Python shell with the file's data loaded as a list of lists. The data is available in the 'rows' variable. ```text $ clevercsv explore milk.csv Dropping you into an interactive shell. CleverCSV has loaded the data into the variable: rows >>> >>> len(rows) 381 ``` -------------------------------- ### Explore CSV Data in Interactive Shell (Pandas DataFrame) Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Explore a CSV file by launching an interactive Python shell with the file's data loaded as a Pandas DataFrame. The data is available in the 'df' variable. ```text $ clevercsv explore -p imdb.csv Dropping you into an interactive shell. CleverCSV has loaded the data into the variable: df >>> >>> df.head() fn tid ... War Western 0 titles01/tt0012349 tt0012349 ... 0 0 1 titles01/tt0015864 tt0015864 ... 0 0 2 titles01/tt0017136 tt0017136 ... 0 0 3 titles01/tt0017925 tt0017925 ... 0 0 4 titles01/tt0021749 tt0021749 ... 0 0 [5 rows x 44 columns] ``` -------------------------------- ### Use CleverCSV as a CSV Module Replacement Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Demonstrates how to use CleverCSV's `Sniffer` and `reader` as a drop-in replacement for Python's built-in `csv` module. This is particularly useful for handling CSV files with non-standard dialects. ```python # Use CleverCSV as drop-in replacement for the Python CSV module # This follows the Sniffer example: https://docs.python.org/3/library/csv.html#csv.Sniffer # Note that csv.Sniffer would fail here with open('./imdb.csv', newline='') as csvfile: dialect = clevercsv.Sniffer().sniff(csvfile.read()) csvfile.seek(0) reader = clevercsv.reader(csvfile, dialect) rows = list(reader) ``` -------------------------------- ### SimpleDialect Methods Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Methods for serializing, deserializing, and validating CSV dialects. ```APIDOC ## SimpleDialect.deserialize() ### Description Deserializes a dialect representation into a SimpleDialect object. ### Method Call ### Parameters None ### Response A SimpleDialect object. ``` ```APIDOC ## SimpleDialect.from_csv_dialect() ### Description Creates a SimpleDialect object from a standard CSV dialect object. ### Method Call ### Parameters None ### Response A SimpleDialect object. ``` ```APIDOC ## SimpleDialect.from_dict() ### Description Creates a SimpleDialect object from a dictionary representation. ### Method Call ### Parameters None ### Response A SimpleDialect object. ``` ```APIDOC ## SimpleDialect.serialize() ### Description Serializes the SimpleDialect object into a representation, likely a dictionary or string. ### Method Call ### Parameters None ### Response A serialized representation of the dialect. ``` ```APIDOC ## SimpleDialect.to_csv_dialect() ### Description Converts the SimpleDialect object to a standard CSV dialect object. ### Method Call ### Parameters None ### Response A CSV dialect object. ``` ```APIDOC ## SimpleDialect.to_dict() ### Description Converts the SimpleDialect object into a dictionary representation. ### Method Call ### Parameters None ### Response A dictionary representation of the dialect. ``` ```APIDOC ## SimpleDialect.validate() ### Description Validates the current SimpleDialect object to ensure it conforms to expected standards. ### Method Call ### Parameters None ### Response Boolean indicating if the dialect is valid. ``` -------------------------------- ### SimpleDialect Class Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Represents a simplified CSV dialect with essential parsing parameters. ```APIDOC ## class clevercsv.dialect.SimpleDialect(delimiter, quotechar, escapechar, strict) ### Description The simplified dialect object. For the delimiter, quotechar, and escapechar the empty string means no delimiter/quotechar/escapechar in the file. None is used to mark it undefined. ### Parameters - **delimiter** (str | None): The delimiter of the CSV file. - **quotechar** (str | None): The quotechar of the file. - **escapechar** (str | None): The escapechar of the file. - **strict** (bool, optional): Whether strict parsing should be enforced. Defaults to False. ### Methods - **deserialize(obj: str)**: Deserialize dialect from a JSON object. - **from_csv_dialect(d)**: Create a SimpleDialect from a csv.Dialect object. - **from_dict(d: Dict[str, Any])**: Create a SimpleDialect from a dictionary. - **serialize()**: Serialize dialect to a JSON object. - **to_csv_dialect()**: Convert the SimpleDialect to a csv.Dialect object. - **to_dict()**: Convert the SimpleDialect to a dictionary. - **validate()**: Validate the dialect settings. ``` -------------------------------- ### CleverCSV Command-Line Tool Usage Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md The clevercsv command-line tool offers various commands for CSV file manipulation, including detection, viewing, standardizing, and code generation. Use `clevercsv help ` for specific command details. ```text usage: clevercsv [-h] [-V] [-v] command ... Available commands: help Display help information detect Detect the dialect of a CSV file view View the CSV file on the command line using TabView standardize Convert a CSV file to one that conforms to RFC-4180 code Generate Python code to import a CSV file explore Explore the CSV file in an interactive Python shell ``` -------------------------------- ### Type Detection Utilities Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Utility functions for generating and scoring data types. ```APIDOC ## gen_known_type() ### Description Generates a known data type, likely for testing or internal use. ### Method Call ### Parameters None ### Response Details about a generated known type. ``` ```APIDOC ## type_score() ### Description Calculates a score for a given data type, possibly indicating its suitability or confidence. ### Method Call ### Parameters None ### Response A score representing the data type's quality or relevance. ``` -------------------------------- ### clevercsv.detect Module Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Core CSV detection functionalities, including different detection methods and a primary Detector class. ```APIDOC ## Detector.detect() ### Description Detects the CSV dialect for the given data. ### Method `clevercsv.detect.Detector.detect(data, n=5)` ### Parameters * **data** (iterable) - The CSV data to analyze. * **n** (int, optional) - The number of top dialects to consider. Defaults to 5. ### Returns * (object) - The detected CSV dialect. ``` ```APIDOC ## Detector.has_header() ### Description Determines if the first row of the CSV data is likely a header. ### Method `clevercsv.detect.Detector.has_header(data, dialect)` ### Parameters * **data** (iterable) - The CSV data to analyze. * **dialect** (object) - The CSV dialect to use. ### Returns * (bool) - True if a header is detected, False otherwise. ``` ```APIDOC ## Detector.sniff() ### Description Sniffs the CSV dialect from a sample of the data. ### Method `clevercsv.detect.Detector.sniff(sample, strict=False)` ### Parameters * **sample** (str) - A sample of CSV data. * **strict** (bool, optional) - Whether to enforce strict parsing rules. Defaults to False. ### Returns * (object) - The sniffed CSV dialect. ``` -------------------------------- ### Load CSV as List of Rows with CleverCSV Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Import the clevercsv package and use `read_table` to load a CSV file into a list of rows. This is useful for processing CSV data row by row. ```python import clevercsv # Load the file as a list of rows # This uses the imdb.csv file in the examples directory rows = clevercsv.read_table('./imdb.csv') ``` -------------------------------- ### Configure CleverCSV Pre-commit Hook Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Configure a pre-commit hook to automatically standardize CSV files to RFC-4180 before committing them to a repository. This ensures that only clean, standard CSVs are committed. ```yaml repos: - repo: https://github.com/alan-turing-institute/CleverCSV-pre-commit rev: v0.6.6 # or any later version hooks: - id: clevercsv-standardize ``` -------------------------------- ### clevercsv.detect_pattern.make_abstraction Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Creates an abstract representation of CSV data based on a given dialect, which is used for computing row patterns. ```APIDOC ## make_abstraction(data: str, dialect: [SimpleDialect](#clevercsv.dialect.SimpleDialect)) -> str ### Description Create an abstract representation of the CSV file based on the dialect. This function constructs the basic abstraction used to compute the row patterns. ### Parameters * **data** (*str*) – The data of the file as a string. * **dialect** ([*SimpleDialect*](#clevercsv.dialect.SimpleDialect)) – A dialect to parse the file with. ### Returns **abstraction** – An abstract representation of the CSV file. ### Return type str ``` -------------------------------- ### Type Detection Utilities Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Utilities for generating and scoring data types within cells. ```APIDOC ## clevercsv.detect_type.gen_known_type(cells) ### Description Utility that yields a generator over whether or not the provided cells are of a known type or not. ### Parameters - **cells**: An iterable of cell contents to check. ``` ```APIDOC ## clevercsv.detect_type.type_score(data, dialect, eps) ### Description Compute the type score as the ratio of cells with a known type. ### Parameters - **data** (str): The data as a single string. - **dialect** ([SimpleDialect](#clevercsv.dialect.SimpleDialect)): The dialect to use for parsing. - **eps** (float, optional): The minimum value of the type score. Defaults to 1e-10. ### Returns - float: The computed type score. ``` -------------------------------- ### Read CSV Data with Automatic Dialect Detection Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Use the clevercsv.reader to read CSV data after detecting the dialect. The file pointer must be reset after reading for detection. ```python import clevercsv with open("data.csv", "r", newline="") as fp: # you can use verbose=True to see what CleverCSV does dialect = clevercsv.Sniffer().sniff(fp.read(), verbose=False) fp.seek(0) reader = clevercsv.reader(fp, dialect) rows = list(reader) ``` -------------------------------- ### Detect CSV Dialect with Plain Output Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Detect the dialect of a CSV file and output its components on separate lines. This format is easier to parse with command-line tools like grep. ```text $ clevercsv detect --plain imdb.csv delimiter = , quotechar = escapechar = \ ``` -------------------------------- ### Generate Python Code to Import CSV with CleverCSV CLI Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/index.md Use the `clevercsv code` command to generate Python code for importing a specific CSV file, including the detected dialect parameters. ```bash clevercsv code ./imdb.csv ``` -------------------------------- ### Use CleverCSV as a CSV Sniffer Replacement Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/index.md This snippet demonstrates using `clevercsv.Sniffer` to detect the dialect of a CSV file, similar to Python's built-in `csv.Sniffer`. It then uses the detected dialect with `clevercsv.reader`. ```python import clevercsv with open('./imdb.csv', newline='') as csvfile: dialect = clevercsv.Sniffer().sniff(csvfile.read()) csvfile.seek(0) reader = clevercsv.reader(csvfile, dialect) rows = list(reader) ``` -------------------------------- ### View CSV File in Terminal Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md View the contents of a CSV file directly in the terminal. The dialect is automatically detected. The `--transpose` flag can be used to view the file transposed. ```bash $ clevercsv view --transpose imdb.csv ``` -------------------------------- ### Generate Python Code for CSV Reading Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/_readme.md Use this command to generate Python code for reading a CSV file with a detected dialect. Copy the output to a Python script for repeated use. ```text $ clevercsv code imdb.csv # Code generated with CleverCSV import clevercsv with open("imdb.csv", "r", newline="", encoding="utf-8") as fp: reader = clevercsv.reader(fp, delimiter=",", quotechar="", escapechar="\") rows = list(reader) ``` -------------------------------- ### List Known Types Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Retrieves a list of all data types that CleverCSV recognizes. ```APIDOC ## list_known_types() ### Description Returns a list of strings, where each string is the name of a known data type. ### Returns - List[str]: A list of known data type names. ``` -------------------------------- ### clevercsv.exceptions.NoDetectionResult Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Exception raised when no dialect detection result is found. ```APIDOC ## clevercsv.exceptions.NoDetectionResult ### Description Exception raised when no dialect detection result is found. ### Bases `Exception` ``` -------------------------------- ### DictReader and DictWriter Classes Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Classes for reading and writing CSV files where each row is a dictionary. ```APIDOC ## class clevercsv.dict_read_write.DictReader(f, fieldnames, restkey, restval, dialect, *args, **kwds) ### Description Reads CSV files and returns each row as a dictionary. This class is based on Python's built-in csv.DictReader. ### Parameters - **f** (Iterable[str]): The file-like object to read from. - **fieldnames** (Sequence[_T] | None, optional): A sequence of strings identifying each column. If None, the first row is used as fieldnames. - **restkey** (str | None, optional): Specifies a key to store leftover fields if a row has more fields than fieldnames. - **restval** (str | None, optional): Specifies a value to use for missing fields if a row has fewer fields than fieldnames. - **dialect** (_DialectLike, optional): The CSV dialect to use. Defaults to 'excel'. - **args**: Additional positional arguments for the underlying reader. - **kwds**: Additional keyword arguments for the underlying reader. ### Properties - **fieldnames**: Sequence[_T] - The sequence of field names. ``` ```APIDOC ## class clevercsv.dict_read_write.DictWriter(f, fieldnames, restval, extrasaction, dialect, *args, **kwds) ### Description Writes rows to CSV files as dictionaries. This class is based on Python's built-in csv.DictWriter. ### Parameters - **f** (SupportsWrite[str]): The file-like object to write to. - **fieldnames** (Collection[_T]): A sequence of strings identifying each column. - **restval** (Any | None, optional): Specifies a value to use for fields not present in the row dictionary. Defaults to ''. - **extrasaction** (Literal['raise', 'ignore'], optional): Specifies how to handle extra fields in the row dictionary that are not in fieldnames. Defaults to 'raise'. - **dialect** (_DialectLike, optional): The CSV dialect to use. Defaults to 'excel'. - **args**: Additional positional arguments for the underlying writer. - **kwds**: Additional keyword arguments for the underlying writer. ### Methods - **writeheader()**: Writes the header row based on the fieldnames. - **writerow(rowdict)**: Writes a single row dictionary to the CSV file. - **writerows(rowdicts)**: Writes multiple row dictionaries to the CSV file. ``` -------------------------------- ### Detect CSV Dialect with CleverCSV CLI Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Use the `clevercsv detect` command to automatically detect the dialect of a CSV file. This is helpful for understanding the structure of messy CSV files. ```bash # Detect the dialect $ clevercsv detect ./imdb.csv ``` -------------------------------- ### clevercsv.detect.detect Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Detects the dialect of a CSV file from a sample of its content. It can consider a specified set of delimiters and use different detection methods. Verbose mode provides more detailed output. ```APIDOC ## detect(sample: str, delimiters: Iterable[str] | None = None, verbose: bool = False, method: [DetectionMethod](#clevercsv.detect.DetectionMethod) | str = DetectionMethod.AUTO, skip: bool = True) -> [SimpleDialect](#clevercsv.dialect.SimpleDialect) | None ### Description Detect the dialect of a CSV file using the specified detection method. ### Parameters * **sample** (*str*) – A sample of text from the CSV file. For best results and if time allows, use the entire contents of the CSV file as the sample. * **delimiters** (*Optional* *[**Iterable* *[**str* *]* *]*) – Set of delimiters to consider for dialect detection. The potential dialects will be constructed by analyzing the sample and these delimiters. If omitted, the set of potential delimiters will be constructed from the sample. * **verbose** (*bool*) – Enable verbose mode. * **method** (*Union* *[*[*DetectionMethod*](#clevercsv.detect.DetectionMethod) *,* *str* *]*) – The method to use for dialect detection. Possible values are [`DetectionMethod`](#clevercsv.detect.DetectionMethod) instances or strings that can be cast to as such an enum. * **skip** (*bool*) – Whether to skip potential dialects that have too low a pattern score in the consistency detection. See `ConsistencyDetector.compute_consistency_scores()` for more details. ### Returns **dialect** – The detected dialect. Can be None if dialect detection was inconclusive. ### Return type Optional[[SimpleDialect](#clevercsv.dialect.SimpleDialect)] ``` -------------------------------- ### clevercsv.exceptions.Error Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Base exception class for CleverCSV errors. ```APIDOC ## clevercsv.exceptions.Error ### Description Base exception class for CleverCSV errors. ### Bases `Error` ``` -------------------------------- ### DictWriter Methods Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Methods for writing data to CSV files using dictionary formats. ```APIDOC ## DictWriter.writeheader() ### Description Writes the header row to the CSV file. ### Method Call ### Parameters None ### Response None. Writes header to the underlying file object. ``` ```APIDOC ## DictWriter.writerow() ### Description Writes a single row of data (as a dictionary) to the CSV file. ### Method Call ### Parameters None ### Response None. Writes a row to the underlying file object. ``` ```APIDOC ## DictWriter.writerows() ### Description Writes multiple rows of data (as a list of dictionaries) to the CSV file. ### Method Call ### Parameters None ### Response None. Writes multiple rows to the underlying file object. ``` -------------------------------- ### clevercsv.Detector.detect Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Detects the dialect of a CSV file from a sample string. ```APIDOC ## clevercsv.Detector.detect ### Description Detect the dialect of a CSV file using the specified detection method. ### Parameters * **sample** (*str*) – A sample of text from the CSV file. For best results and if time allows, use the entire contents of the CSV file as the sample. * **delimiters** (*Optional[Iterable[str]] = None*) – Set of delimiters to consider for dialect detection. The potential dialects will be constructed by analyzing the sample and these delimiters. If omitted, the set of potential delimiters will be constructed from the sample. * **verbose** (*bool = False*) – Enable verbose mode. * **method** (*Union[DetectionMethod, str] = DetectionMethod.AUTO*) – The method to use for dialect detection. Possible values are `DetectionMethod` instances or strings that can be cast to as such an enum. * **skip** (*bool = True*) – Whether to skip potential dialects that have too low a pattern score in the consistency detection. See `ConsistencyDetector.compute_consistency_scores()` for more details. ### Returns **dialect** – The detected dialect. Can be None if dialect detection was inconclusive. ### Return type Optional[SimpleDialect] ``` -------------------------------- ### clevercsv.detect_pattern Module Functions Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Functions related to pattern detection in CSV data, useful for data cleaning and abstraction. ```APIDOC ## fill_empties() ### Description Fills empty fields in CSV data based on detected patterns. ### Method `clevercsv.detect_pattern.fill_empties(data, dialect)` ### Parameters * **data** (iterable) - The CSV data. * **dialect** (object) - The CSV dialect. ### Returns * (list) - Data with empty fields filled. ``` ```APIDOC ## make_abstraction() ### Description Creates an abstraction of the CSV data, potentially simplifying patterns. ### Method `clevercsv.detect_pattern.make_abstraction(data, dialect)` ### Parameters * **data** (iterable) - The CSV data. * **dialect** (object) - The CSV dialect. ### Returns * (object) - An abstracted representation of the data. ``` ```APIDOC ## merge_with_quotechar() ### Description Merges fields in CSV data, considering the quote character. ### Method `clevercsv.detect_pattern.merge_with_quotechar(data, dialect)` ### Parameters * **data** (iterable) - The CSV data. * **dialect** (object) - The CSV dialect. ### Returns * (list) - Data with fields potentially merged based on quote character. ``` ```APIDOC ## pattern_score() ### Description Scores the pattern of a given CSV dialect against the data. ### Method `clevercsv.detect_pattern.pattern_score(data, dialect)` ### Parameters * **data** (iterable) - The CSV data. * **dialect** (object) - The CSV dialect to score. ### Returns * (float) - The pattern score. ``` ```APIDOC ## strip_trailing() ### Description Strips trailing whitespace or characters from fields in CSV data. ### Method `clevercsv.detect_pattern.strip_trailing(data, dialect)` ### Parameters * **data** (iterable) - The CSV data. * **dialect** (object) - The CSV dialect. ### Returns * (list) - Data with trailing characters stripped. ``` -------------------------------- ### clevercsv.write.writer Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md A drop-in replacement for the Python csv writer class. ```APIDOC ## class clevercsv.write.writer ### Description Drop-in replacement for the Python csv writer class. ### Methods #### writerow(row: Iterable[Any]) → Any Writes a single row to the CSV file. #### writerows(rows: Iterable[Iterable[Any]]) → Any Writes multiple rows to the CSV file. ``` -------------------------------- ### clevercsv.wrappers.write_dicts Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Writes a list of dictionaries to a CSV file. Each dictionary represents a row, and keys are used as headers. ```APIDOC ## write_dicts() ### Description Writes a list of dictionaries to a CSV file. ### Method (Not specified, likely a function call) ### Endpoint (Not applicable, this is a Python function) ### Parameters (Parameters not explicitly detailed in the source) ### Request Example (Not applicable) ### Response (Response details not explicitly detailed in the source) ``` -------------------------------- ### Detect CSV Dialect Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/index.md Detects the dialect of a CSV file and outputs the detected parameters. The --plain flag provides each parameter on a separate line for easier parsing. ```text $ clevercsv detect imdb.csv Detected: SimpleDialect(',', '', '\') ``` ```text $ clevercsv detect --plain imdb.csv delimiter = , quotechar = escapechar = \ ``` -------------------------------- ### clevercsv.detect_type Module Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Utilities for detecting the data types of fields within CSV columns. ```APIDOC ## TypeDetector.detect_type() ### Description Detects the most likely data type for a given value. ### Method `clevercsv.detect_type.TypeDetector.detect_type(value)` ### Parameters * **value** (str) - The string value to detect the type of. ### Returns * (str) - The detected data type (e.g., 'number', 'date', 'email'). ``` ```APIDOC ## TypeDetector.is_bytearray() ### Description Checks if a value represents a bytearray. ### Method `clevercsv.detect_type.TypeDetector.is_bytearray(value)` ### Parameters * **value** (str) - The string value to check. ### Returns * (bool) - True if the value is a bytearray, False otherwise. ``` ```APIDOC ## TypeDetector.is_currency() ### Description Checks if a value represents a currency amount. ### Method `clevercsv.detect_type.TypeDetector.is_currency(value)` ### Parameters * **value** (str) - The string value to check. ### Returns * (bool) - True if the value is currency, False otherwise. ``` ```APIDOC ## TypeDetector.is_date() ### Description Checks if a value represents a date. ### Method `clevercsv.detect_type.TypeDetector.is_date(value)` ### Parameters * **value** (str) - The string value to check. ### Returns * (bool) - True if the value is a date, False otherwise. ``` ```APIDOC ## TypeDetector.is_datetime() ### Description Checks if a value represents a datetime. ### Method `clevercsv.detect_type.TypeDetector.is_datetime(value)` ### Parameters * **value** (str) - The string value to check. ### Returns * (bool) - True if the value is a datetime, False otherwise. ``` ```APIDOC ## TypeDetector.is_email() ### Description Checks if a value represents an email address. ### Method `clevercsv.detect_type.TypeDetector.is_email(value)` ### Parameters * **value** (str) - The string value to check. ### Returns * (bool) - True if the value is an email address, False otherwise. ``` ```APIDOC ## TypeDetector.is_empty() ### Description Checks if a value is considered empty. ### Method `clevercsv.detect_type.TypeDetector.is_empty(value)` ### Parameters * **value** (str) - The string value to check. ### Returns * (bool) - True if the value is empty, False otherwise. ``` ```APIDOC ## TypeDetector.is_ipv4() ### Description Checks if a value represents an IPv4 address. ### Method `clevercsv.detect_type.TypeDetector.is_ipv4(value)` ### Parameters * **value** (str) - The string value to check. ### Returns * (bool) - True if the value is an IPv4 address, False otherwise. ``` ```APIDOC ## TypeDetector.is_json_obj() ### Description Checks if a value represents a JSON object. ### Method `clevercsv.detect_type.TypeDetector.is_json_obj(value)` ### Parameters * **value** (str) - The string value to check. ### Returns * (bool) - True if the value is a JSON object, False otherwise. ``` ```APIDOC ## TypeDetector.is_known_type() ### Description Checks if a value matches any of the known data types. ### Method `clevercsv.detect_type.TypeDetector.is_known_type(value)` ### Parameters * **value** (str) - The string value to check. ### Returns * (bool) - True if the value matches a known type, False otherwise. ``` ```APIDOC ## TypeDetector.is_nan() ### Description Checks if a value represents 'Not a Number' (NaN). ### Method `clevercsv.detect_type.TypeDetector.is_nan(value)` ### Parameters * **value** (str) - The string value to check. ### Returns * (bool) - True if the value is NaN, False otherwise. ``` ```APIDOC ## TypeDetector.is_number() ### Description Checks if a value represents a number. ### Method `clevercsv.detect_type.TypeDetector.is_number(value)` ### Parameters * **value** (str) - The string value to check. ### Returns * (bool) - True if the value is a number, False otherwise. ``` ```APIDOC ## TypeDetector.is_percentage() ### Description Checks if a value represents a percentage. ### Method `clevercsv.detect_type.TypeDetector.is_percentage(value)` ### Parameters * **value** (str) - The string value to check. ### Returns * (bool) - True if the value is a percentage, False otherwise. ``` -------------------------------- ### clevercsv.unix_dialect Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Predefined dialect for Unix-style CSV files. Contains common settings for Unix-like environments. ```APIDOC ## unix_dialect ### Description Predefined dialect for Unix-style CSV files. ### Attributes - **delimiter** (string) - The delimiter character. - **doublequote** (boolean) - Whether to double quote fields containing the quote character. - **lineterminator** (string) - The character(s) used to terminate lines. - **quotechar** (string) - The character used to quote fields containing special characters. - **quoting** (integer) - The quoting strategy. - **skipinitialspace** (boolean) - Whether to ignore whitespace after the delimiter. ``` -------------------------------- ### Generate Python Code to Import CSV with CleverCSV CLI Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Use the `clevercsv code` command to generate Python code for importing a specific CSV file. This code snippet can be directly used in your Python scripts. ```bash # Generate code to import the file $ clevercsv code ./imdb.csv ``` -------------------------------- ### clevercsv.consistency.ConsistencyDetector Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Detects CSV dialects using the data consistency measure. This class allows for fine-grained control over the detection process, including options to skip dialects with low pattern scores and to enable verbose output. ```APIDOC ## clevercsv.consistency.ConsistencyDetector ### Description Detect the dialect with the data consistency measure. This class uses the data consistency measure to detect the dialect. See the paper for details. ### Parameters * **skip** (*bool*) – Skip computation of the type score for dialects with a low pattern score. * **verbose** (*bool*) – Print out the dialects considered and their scores. * **cache_capacity** (*int*) – The size of the cache for type detection. Caching the type detection result greatly speeds up the computation of the consistency measure. The size of the cache can be changed to trade off memory use and speed. ``` -------------------------------- ### clevercsv.excel_tab Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Predefined dialect for tab-separated files, often used by Excel. Similar to `clevercsv.excel` but with a tab delimiter. ```APIDOC ## excel_tab ### Description Predefined dialect for tab-separated files, often used by Excel. ### Attributes - **delimiter** (string) - The delimiter character (tab). ``` -------------------------------- ### clevercsv.write_table Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Writes a table (represented as a list of lists) to a CSV file. If the table is empty, no file is created. This function simplifies the process of saving structured data to a CSV format. ```APIDOC ## clevercsv.write_table ### Description Write a table (a list of lists) to a file. This is a convenience function for writing a table to a CSV file. If the table has no rows, no output file is created. ### Parameters * **table** (*list*) – A table as a list of lists. The table must have the same number of cells in each row (taking the `transpose` flag into account). * **filename** (*str*) – The filename of the CSV file to write the table to. * **dialect** ([*SimpleDialect*](#clevercsv.dialect.SimpleDialect) *or* *csv.Dialect*) – The dialect to use. The default is the ‘excel’ dialect, which corresponds to RFC4180. This is done to encourage more standardized CSV files. * **transpose** (*bool*) – Transpose the table before writing. * **encoding** (*str*) – Encoding to use to write the data to the file. Note that the default encoding is platform dependent, which ensures compatibility with the Python open() function. It thus defaults to locale.getpreferredencoding(). ### Raises **ValueError:** – When the length of the rows is not constant. ``` -------------------------------- ### clevercsv.wrappers.write_table Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Writes a list of lists (table format) to a CSV file. Each inner list corresponds to a row. ```APIDOC ## write_table() ### Description Writes a list of lists (table format) to a CSV file. ### Method (Not specified, likely a function call) ### Endpoint (Not applicable, this is a Python function) ### Parameters (Parameters not explicitly detailed in the source) ### Request Example (Not applicable) ### Response (Response details not explicitly detailed in the source) ``` -------------------------------- ### Data Type Detection Functions Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md A collection of functions to determine if a given cell's content matches specific data types. ```APIDOC ## is_email ### Description Checks if a cell's content is a valid email address. ### Parameters - **cell** (str) - The string content of the cell. - **is_quoted** (bool, optional) - Whether the cell content was quoted. Defaults to False. ### Returns - bool: True if the cell is an email, False otherwise. ``` ```APIDOC ## is_empty ### Description Checks if a cell's content is empty. ### Parameters - **cell** (str) - The string content of the cell. - **is_quoted** (bool, optional) - Whether the cell content was quoted. Defaults to False. ### Returns - bool: True if the cell is empty, False otherwise. ``` ```APIDOC ## is_ipv4 ### Description Checks if a cell's content is a valid IPv4 address. ### Parameters - **cell** (str) - The string content of the cell. - **is_quoted** (bool, optional) - Whether the cell content was quoted. Defaults to False. ### Returns - bool: True if the cell is an IPv4 address, False otherwise. ``` ```APIDOC ## is_json_obj ### Description Checks if a cell's content is a valid JSON object. ### Parameters - **cell** (str) - The string content of the cell. - **is_quoted** (bool, optional) - Whether the cell content was quoted. Defaults to False. ### Returns - bool: True if the cell is a JSON object, False otherwise. ``` ```APIDOC ## is_known_type ### Description Checks if a cell's content matches any of the known data types. ### Parameters - **cell** (str) - The string content of the cell. - **is_quoted** (bool, optional) - Whether the cell content was quoted. Defaults to False. ### Returns - bool: True if the cell matches a known type, False otherwise. ``` ```APIDOC ## is_nan ### Description Checks if a cell's content represents 'Not a Number' (NaN). ### Parameters - **cell** (str) - The string content of the cell. - **is_quoted** (bool, optional) - Whether the cell content was quoted. Defaults to False. ### Returns - bool: True if the cell is NaN, False otherwise. ``` ```APIDOC ## is_number ### Description Checks if a cell's content can be interpreted as a number. ### Parameters - **cell** (str) - The string content of the cell. - **is_quoted** (bool, optional) - Whether the cell content was quoted. Defaults to False. ### Returns - bool: True if the cell is a number, False otherwise. ``` ```APIDOC ## is_percentage ### Description Checks if a cell's content represents a percentage. ### Parameters - **cell** (str) - The string content of the cell. - **is_quoted** (bool, optional) - Whether the cell content was quoted. Defaults to False. ### Returns - bool: True if the cell is a percentage, False otherwise. ``` ```APIDOC ## is_time ### Description Checks if a cell's content represents a time. ### Parameters - **cell** (str) - The string content of the cell. - **is_quoted** (bool, optional) - Whether the cell content was quoted. Defaults to False. ### Returns - bool: True if the cell is a time, False otherwise. ``` ```APIDOC ## is_unicode_alphanum ### Description Checks if a cell's content consists of Unicode alphanumeric characters. ### Parameters - **cell** (str) - The string content of the cell. - **is_quoted** (bool, optional) - Whether the cell content was quoted. Defaults to False. ### Returns - bool: True if the cell is Unicode alphanumeric, False otherwise. ``` ```APIDOC ## is_unix_path ### Description Checks if a cell's content is a valid Unix-style file path. ### Parameters - **cell** (str) - The string content of the cell. - **is_quoted** (bool, optional) - Whether the cell content was quoted. Defaults to False. ### Returns - bool: True if the cell is a Unix path, False otherwise. ``` ```APIDOC ## is_url ### Description Checks if a cell's content is a valid URL. ### Parameters - **cell** (str) - The string content of the cell. - **is_quoted** (bool, optional) - Whether the cell content was quoted. Defaults to False. ### Returns - bool: True if the cell is a URL, False otherwise. ``` -------------------------------- ### Encoding Module Function Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Function for detecting the encoding of a file. ```APIDOC ## get_encoding() ### Description Detects the character encoding of a given file or byte stream. ### Method Call ### Parameters None ### Response The detected encoding string. ``` -------------------------------- ### Standardize CSV to RFC-4180 Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/README.md Rewrite a CSV file to conform to the RFC-4180 standard. This command is useful for ensuring data consistency and compatibility. ```bash $ clevercsv standardize --output imdb_standard.csv imdb.csv ``` -------------------------------- ### clevercsv.Detector.detect Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/modules.md Detects CSV dialect properties from a file or string. This method is part of the Detector class. ```APIDOC ## Detector.detect() ### Description Detects CSV dialect properties from a file or string. ### Method (Not specified, likely a method call on a Detector instance) ### Endpoint (Not applicable, this is a Python method) ### Parameters (Parameters not explicitly detailed in the source) ### Request Example (Not applicable) ### Response (Response details not explicitly detailed in the source) ``` -------------------------------- ### clevercsv.potential_dialects.get_delimiters Source: https://github.com/alan-turing-institute/clevercsv/blob/master/docs/source/clevercsv.md Identifies potential delimiters within a given dataset, considering Unicode character categories and explicitly blocked characters. ```APIDOC ## clevercsv.potential_dialects.get_delimiters ### Description Get potential delimiters for a CSV file. ### Parameters * **data** (*str*) – The data of the file. * **encoding** (*str*) – The encoding of the file. * **delimiters** (*List[str] | None*) – Allowed delimiters. If provided, it overrides the block_cat/block_char mechanism and only the provided characters will be considered delimiters (if they occur in the file). If None, all characters can be considered delimiters subject to the `block_cat` and `block_char` parameters. * **block_cat** (*List[str] | None*) – List of Unicode categories (2-letter abbreviations) for characters that should not be considered as delimiters. If None, a default set is used. * **block_char** (*List[str] | None*) – Explicit list of characters that should not be considered delimiters. If None, a default set is used. ### Returns * **delims** (*Set[str]*) – Set of potential delimiters. The empty string is added by default. ```