Skip to main content

chat1

def python_cut(file_path, delimiter, list_of_cols):
    """
    Emulates the 'cut' command in Python, for files only.
    
    Args:
    - file_path (str): Path to the input file.
    - delimiter (str): The delimiter character.
    - list_of_cols (list): List of column indices to extract.
    """
    try:
        with open(file_path, 'r') as file:
            for line in file:
                # Split the line using the specified delimiter
                fields = line.strip().split(delimiter)
                
                # Select the specified columns, adjusting for 0-based index
                selected_fields = [fields[i-1] for i in list_of_cols if i-1 < len(fields)]
                
                # Join the selected fields with the delimiter and print
                print(delimiter.join(selected_fields))

    except Exception as e:
        print(f"Error: {e}")

# Example usage:
# python_cut('path_to_your_file.txt', ';', [6, 9])