Advertisement
Python253

user_encapsulator_preserve_selected

Mar 21st, 2024
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Filename: user_encapsulator_preserve_selected.py
  3. # Version: 1.00
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. USER ENCAPSULATOR PRESERVE SELECTED:
  8.  
  9. This script encapsulates specified lines of a text file based on user input,
  10. saving both the original text and 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. # Open the input file in read mode
  58. with open(input_file, "r") as file:
  59.     # Read all lines from the file
  60.     all_lines = file.readlines()
  61.  
  62. # If user chooses to process specific line numbers
  63. if option == '2':
  64.     # Ask user for specific line numbers
  65.     line_numbers = get_user_input_for_line_numbers()
  66.    
  67.     # Display selected lines with line numbers
  68.     print("\nSelected lines:")
  69.     for num in line_numbers:
  70.         print(f"{num}. {all_lines[num - 1].strip()}")
  71.  
  72.     while True:
  73.         # Display continue options
  74.         print("\nContinue options:")
  75.         display_options(["1. Continue with Encapsulation", "2. Go back: Edit line numbers", "3. Exit Program"])
  76.        
  77.         # Ask user if they want to continue, edit line numbers, or exit
  78.         choice = input("Choose an option: ")
  79.         if choice == '1':
  80.             break
  81.         elif choice == '2':
  82.             line_numbers = get_user_input_for_line_numbers()
  83.             print("\nSelected lines:")
  84.             for num in line_numbers:
  85.                 print(f"{num}. {all_lines[num - 1].strip()}")
  86.         elif choice == '3':
  87.             sys.exit(0)
  88.         else:
  89.             print("Invalid choice. Exiting...")
  90.             sys.exit(0)
  91. elif option == '1':
  92.     print("All lines have been encapsulated.")
  93. else:
  94.     print("Invalid option selected. Exiting...")
  95.     sys.exit(0)
  96.  
  97. # Open the output file in write mode
  98. with open("encapsulated_output.txt", "w") as file:
  99.     # Iterate over each line in the all lines
  100.     for i, line in enumerate(all_lines, 1):
  101.         # Remove the newline character at the end of the line
  102.         line = line.strip()
  103.         if i in line_numbers:
  104.             # Encapsulate the selected line with user-defined left and right encapsulation using string formatting
  105.             modified_line = "{}{}{}".format(left_encapsulation, line, right_encapsulation)
  106.         else:
  107.             # Otherwise, keep the line as it is
  108.             modified_line = line
  109.         # Write the modified line to the output file
  110.         file.write(modified_line + "\n")
  111.  
  112. print("Output has been written to encapsulated_output.txt.")
  113. sys.exit(0)
  114.  
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement