Advertisement
Python253

user_encapsulator_only_selected

Mar 21st, 2024
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.85 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Filename: user_encapsulator_only_selected.py
  3. # Version: 1.00
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. USER ENCAPSULATOR ONLY SELECTED:
  8.  
  9. This script encapsulates specified lines of a text file based on user input,
  10. saving only the encapsulated text to an output file.
  11. """
  12.  
  13. import sys
  14.  
  15. # Define the input file name
  16. input_file = "1234.txt"
  17.  
  18. # Get left and right encapsulation from user input
  19. left_encapsulation = input("Enter the left side encapsulation: ")
  20. right_encapsulation = input("Enter the right side encapsulation: ")
  21.  
  22. def display_lines_with_numbers(lines):
  23.     """Display lines with their line numbers.
  24.  
  25.    Args:
  26.        lines (list): List of lines to display.
  27.    """
  28.     for i, line in enumerate(lines, 1):
  29.         print(f"{i}. {line.strip()}")
  30.  
  31. def get_user_input_for_line_numbers():
  32.     """Get line numbers input from the user.
  33.  
  34.    Returns:
  35.        list: List of selected line numbers.
  36.    """
  37.     line_numbers = input("Enter the line numbers you want to process separated by commas (Eg; 1, 4, 22, 17): ")
  38.     return [int(num.strip()) for num in line_numbers.split(",")]
  39.  
  40. def display_options(options):
  41.     """Display options as a numbered list.
  42.  
  43.    Args:
  44.        options (list): List of options to display.
  45.    """
  46.     for i, option in enumerate(options, 1):
  47.         print(f"{i}. {option}")
  48.  
  49. # Display options for processing lines
  50. print("\nOptions:")
  51. print("1. Process all lines")
  52. print("2. Process specific line numbers")
  53.  
  54. # Ask user to choose an option
  55. option = input("Choose an option (1 or 2): ")
  56.  
  57. # If user chooses to process specific line numbers
  58. if option == '2':
  59.     # Ask user for specific line numbers
  60.     line_numbers = get_user_input_for_line_numbers()
  61.  
  62.     # Open the input file in read mode
  63.     with open(input_file, "r") as file:
  64.         # Read all lines from the file
  65.         all_lines = file.readlines()
  66.    
  67.     # Filter lines based on user-specified line numbers
  68.     lines = [all_lines[i - 1] for i in line_numbers]
  69.  
  70.     # Display selected lines with line numbers
  71.     print("\nSelected lines:")
  72.     for num, line in zip(line_numbers, lines):
  73.         print(f"{num}. {line.strip()}")
  74.  
  75.     while True:
  76.         # Display continue options
  77.         print("\nContinue options:")
  78.         display_options(["1. Continue with Encapsulation", "2. Go back: Edit line numbers", "3. Exit Program"])
  79.         # Ask user if they want to continue, edit line numbers, or exit
  80.         choice = input("Choose an option: ")
  81.         if choice == '1':
  82.             break
  83.         elif choice == '2':
  84.             line_numbers = get_user_input_for_line_numbers()
  85.             lines = [all_lines[i - 1] for i in line_numbers]
  86.             print("\nSelected lines:")
  87.             for num, line in zip(line_numbers, lines):
  88.                 print(f"{num}. {line.strip()}")
  89.         elif choice == '3':
  90.             sys.exit(0)
  91.         else:
  92.             print("Invalid choice. Exiting...")
  93.             sys.exit(0)
  94. elif option == '1':
  95.     # Open the input file in read mode
  96.     with open(input_file, "r") as file:
  97.         # Read all lines from the file
  98.         lines = file.readlines()
  99.     print("All lines have been encapsulated.")
  100. else:
  101.     print("Invalid option selected. Exiting...")
  102.     sys.exit(0)
  103.  
  104. # Open the output file in write mode
  105. with open("encapsulated_output.txt", "w") as file:
  106.     # Iterate over each line in the all lines
  107.     for line in lines:
  108.         # Remove the newline character at the end of the line
  109.         line = line.strip()
  110.         # Encapsulate the line with user-defined left and right encapsulation using string formatting
  111.         modified_line = "{}{}{}".format(left_encapsulation, line, right_encapsulation)
  112.         # Write the modified line to the output file
  113.         file.write(modified_line + "\n")
  114.  
  115. print("Only encapsulated lines have been written to encapsulated_output.txt.")
  116. sys.exit(0)
  117.  
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement