Advertisement
Python253

text_skip_extractor

Jun 4th, 2024
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.96 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: text_skip_extractor.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script extracts every 'n-th' letter from a text file, ignoring non-alphanumeric characters.
  10.    - The user is prompted to select a file from the file manager and input a skip value.
  11.    - The extracted letters are saved to a new file with a dynamically generated name based on the skip value.
  12.    
  13. Requirements:
  14.    - Python 3.x
  15.    - tkinter for file dialog
  16.  
  17. Functions:
  18.    - extract_letters(text, skip):
  19.        Extracts every 'skip' letter from the provided text.
  20.    - main():
  21.        Main function to execute the letter extraction process.
  22.  
  23. Usage:
  24.    - Run the script in a Python environment with the following command:
  25.    
  26.                'python text_skip_extractor.py'
  27.                
  28.    - Follow the prompts to select a file and enter the skip value.
  29.  
  30. Example Output:
  31.  
  32.        Press Enter to open the file manager and select a file...
  33.  
  34.        Enter the shift value (positive integer): 7
  35.  
  36.        Extracted text:
  37.                         THISWASATEST
  38.  
  39.        Extraction completed. The extracted letters are saved to shifted_7.txt.
  40.  
  41.                    Exiting Program...   GoodBye!
  42.  
  43. Additional Notes:
  44.    - Ensure the file to be processed is a text file encoded in UTF-8.
  45.    - The skip value should be a positive integer.
  46. """
  47.  
  48. import tkinter as tk
  49. from tkinter import filedialog
  50.  
  51. def extract_letters(text, skip):
  52.     """
  53.    Extracts every 'skip' letter from the provided text, ignoring non-alphanumeric characters.
  54.    
  55.    Parameters:
  56.    text (str): The input text from which letters are to be extracted.
  57.    skip (int): The interval at which letters are extracted.
  58.    
  59.    Returns:
  60.    str: A string containing every 'skip'th letter from the input text.
  61.    """
  62.     extracted = ""
  63.     count = 0
  64.     for char in text:
  65.         if char.isalnum():  # Check if the character is alphanumeric
  66.             count += 1
  67.             if count % skip == 0:
  68.                 extracted += char
  69.     return extracted
  70.  
  71. def main():
  72.     """
  73.    Main function to execute the letter extraction process:
  74.        1. Prompts the user to open the file manager and select a text file.
  75.        2. Reads the content of the selected file.
  76.        3. Prompts the user to input a skip value.
  77.        4. Extracts letters from the file content based on the skip value.
  78.        5. Writes the extracted letters to a dynamically named file based on the skip value.
  79.        6. Prints the extracted letters to the terminal.
  80.    """
  81.     # Prompt the user to hit Enter to open the file manager
  82.     input("\nPress Enter to open the file manager and select a file...")
  83.  
  84.     # Open the file manager and let the user select a file
  85.     root = tk.Tk()
  86.     root.withdraw()  # Hide the main window
  87.     file_path = filedialog.askopenfilename()
  88.  
  89.     # Check if a file was selected
  90.     if not file_path:
  91.         print("\nNo file selected!\n")
  92.         return
  93.  
  94.     # Read the content of the selected file
  95.     with open(file_path, 'r', encoding='UTF-8') as file:
  96.         file_content = file.read()
  97.  
  98.     # Prompt the user to input the shift value
  99.     skip_value = int(input("\nEnter the shift value (positive integer): "))
  100.  
  101.     # Extract letters based on the skip value, ignoring punctuation and spaces
  102.     extracted_text = extract_letters(file_content, skip_value)
  103.  
  104.     # Dynamically generate the output filename based on the skip value
  105.     output_filename = f"shifted_{skip_value}.txt"
  106.  
  107.     # Write the extracted letters to a new file
  108.     with open(output_filename, 'w', encoding='utf-8') as output_file:
  109.         output_file.write(extracted_text)
  110.  
  111.     # Print the extracted text to the terminal
  112.     print("\nExtracted text:\n\t\t", extracted_text)
  113.     print(f"\nExtraction completed. The extracted letters are saved to {output_filename}.")
  114.     print("\n\t\tExiting Program...   GoodBye!\n")
  115.  
  116. if __name__ == "__main__":
  117.     main()
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement