DEV Community

Kathryn DiPippo
Kathryn DiPippo

Posted on • Edited on

My go-to Python snippets

My list of go-to Python snippets that I frequently look up, all in one place! Most of the time, I'm just searching for how to parse files into Pythonic objects.

Table of Contents

Read File Into Words

def read_file_into_words(filename: str) -> list[str]:
    """Parse a text file into a 2D list of words, i.e.
        "This is an example file with
        multiple lines of text" becomes
        ["This is an example file with", "multiple lines of text"].

    Args:
        filename (str): Path to text file.

    Returns:
        list[list[str]]: 2D list of string words.
    """
    with open(filename, "r", encoding="utf-8") as input_file:
        content = input_file.readlines()
    content = [word.strip() for word in content]
    return content
Enter fullscreen mode Exit fullscreen mode

Read JSON File into Dict

import json

def read_json_file_into_dict(filename: str) -> dict:
    """Parse a JSON file into a dictionary.

    Args:
        filename (str): Path to JSON file.

    Returns:
        dict: Dictionary representation of JSON file contents.
    """
    with open(filename, "r", encoding="utf-8") as json_file:
        content = json.load(json_file)
    return content
Enter fullscreen mode Exit fullscreen mode

Read CSV File into Dict List

import csv

def read_csv_file_into_dict_list(filename: str) -> list[dict]:
    """Parse a CSV file into a list of dictionaries.

    Args:
        filename (str): Path to CSV file.

    Returns:
        dict: List of dictionary representations of CSV file contents.
    """
    content = []
    with open(filename, newline="", encoding="utf-8") as csv_file:
        reader = csv.DictReader(csv_file)
        for row in reader:
            content.append(dict(row))
    return content
Enter fullscreen mode Exit fullscreen mode

Read CSV File into Dict List without "\ufeff"

Modification of the previous snippet with an additional method that removes the \ufeff special character if present in the column name.

import csv

def rename_dict_key(input_dict: dict, old_key: str, new_key: str) -> dict:
    """Rename key in dictionary while preserving original key order.

    Args:
        input_dict (dict): Dictionary to apply key rename.
        old_key (str): Current name of key.
        new_key (str): New name of key.

    Returns:
        dict: Dictionary with applied key rename.
    """
    return {key if key != old_key else new_key: value for key, value in input_dict.items()}

def read_csv_file_into_dict_list(filename: str) -> list[dict]:
    """Parse a CSV file into a list of dictionaries.

    Args:
        filename (str): Path to CSV file.

    Returns:
        dict: List of dictionary representations of CSV file contents.
    """
    text_content = read_file_into_words(filename)
    print(text_content[0])

    content = []
    with open(filename, newline="", encoding="utf-8") as csv_file:
        reader = csv.DictReader(csv_file)
        for row in reader:
            row_dict = dict(row)
            row_keys_to_rename = [current_key for current_key in row_dict.keys() if "\ufeff" in current_key]

            for row_key_to_rename in row_keys_to_rename:
                row_dict = rename_dict_key(row_dict, row_key_to_rename, row_key_to_rename.replace("\ufeff", ""))

            content.append(row_dict)
    return content
Enter fullscreen mode Exit fullscreen mode

Read YAML File into Dict

def read_yaml_into_dict(filename: str) -> dict:
    """Return content of YAML file as dictionary.

    Args:
        filename (str): Path to YAML file to read and return.

    Returns:
        dict: Content of YAML file as dictionary.
    """
    with open(filename, "r", encoding="utf-8") as yaml_file:
        yaml_contents = yaml_file.read()
    return yaml.safe_load(yaml_contents)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)