Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: text_skip_extractor.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script extracts every 'n-th' letter from a text file, ignoring non-alphanumeric characters.
- - The user is prompted to select a file from the file manager and input a skip value.
- - The extracted letters are saved to a new file with a dynamically generated name based on the skip value.
- Requirements:
- - Python 3.x
- - tkinter for file dialog
- Functions:
- - extract_letters(text, skip):
- Extracts every 'skip' letter from the provided text.
- - main():
- Main function to execute the letter extraction process.
- Usage:
- - Run the script in a Python environment with the following command:
- 'python text_skip_extractor.py'
- - Follow the prompts to select a file and enter the skip value.
- Example Output:
- Press Enter to open the file manager and select a file...
- Enter the shift value (positive integer): 7
- Extracted text:
- THISWASATEST
- Extraction completed. The extracted letters are saved to shifted_7.txt.
- Exiting Program... GoodBye!
- Additional Notes:
- - Ensure the file to be processed is a text file encoded in UTF-8.
- - The skip value should be a positive integer.
- """
- import tkinter as tk
- from tkinter import filedialog
- def extract_letters(text, skip):
- """
- Extracts every 'skip' letter from the provided text, ignoring non-alphanumeric characters.
- Parameters:
- text (str): The input text from which letters are to be extracted.
- skip (int): The interval at which letters are extracted.
- Returns:
- str: A string containing every 'skip'th letter from the input text.
- """
- extracted = ""
- count = 0
- for char in text:
- if char.isalnum(): # Check if the character is alphanumeric
- count += 1
- if count % skip == 0:
- extracted += char
- return extracted
- def main():
- """
- Main function to execute the letter extraction process:
- 1. Prompts the user to open the file manager and select a text file.
- 2. Reads the content of the selected file.
- 3. Prompts the user to input a skip value.
- 4. Extracts letters from the file content based on the skip value.
- 5. Writes the extracted letters to a dynamically named file based on the skip value.
- 6. Prints the extracted letters to the terminal.
- """
- # Prompt the user to hit Enter to open the file manager
- input("\nPress Enter to open the file manager and select a file...")
- # Open the file manager and let the user select a file
- root = tk.Tk()
- root.withdraw() # Hide the main window
- file_path = filedialog.askopenfilename()
- # Check if a file was selected
- if not file_path:
- print("\nNo file selected!\n")
- return
- # Read the content of the selected file
- with open(file_path, 'r', encoding='UTF-8') as file:
- file_content = file.read()
- # Prompt the user to input the shift value
- skip_value = int(input("\nEnter the shift value (positive integer): "))
- # Extract letters based on the skip value, ignoring punctuation and spaces
- extracted_text = extract_letters(file_content, skip_value)
- # Dynamically generate the output filename based on the skip value
- output_filename = f"shifted_{skip_value}.txt"
- # Write the extracted letters to a new file
- with open(output_filename, 'w', encoding='utf-8') as output_file:
- output_file.write(extracted_text)
- # Print the extracted text to the terminal
- print("\nExtracted text:\n\t\t", extracted_text)
- print(f"\nExtraction completed. The extracted letters are saved to {output_filename}.")
- print("\n\t\tExiting Program... GoodBye!\n")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement