Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Filename: user_encapsulator_preserve_selected.py
- # Version: 1.00
- # Author: Jeoi Reqi
- """
- USER ENCAPSULATOR PRESERVE SELECTED:
- This script encapsulates specified lines of a text file based on user input,
- saving both the original text and encapsulated text to an output file.
- """
- import sys
- # Define the input file name
- input_file = "1234.txt"
- # Get left and right encapsulation from user input
- left_encapsulation = input("Enter the left side encapsulation: ")
- right_encapsulation = input("Enter the right side encapsulation: ")
- def display_lines_with_numbers(lines):
- """Display lines with their line numbers.
- Args:
- lines (list): List of lines to display.
- """
- for i, line in enumerate(lines, 1):
- print(f"{i}. {line.strip()}")
- def get_user_input_for_line_numbers():
- """Get line numbers input from the user.
- Returns:
- list: List of selected line numbers.
- """
- line_numbers = input("Enter the line numbers you want to process separated by commas (Eg; 1, 4, 22, 17): ")
- return [int(num.strip()) for num in line_numbers.split(",")]
- def display_options(options):
- """Display options as a numbered list.
- Args:
- options (list): List of options to display.
- """
- for i, option in enumerate(options, 1):
- print(f"{i}. {option}")
- # Display options for processing lines
- print("\nOptions:")
- print("1. Process all lines")
- print("2. Process specific line numbers")
- # Ask user to choose an option
- option = input("Choose an option (1 or 2): ")
- # Open the input file in read mode
- with open(input_file, "r") as file:
- # Read all lines from the file
- all_lines = file.readlines()
- # If user chooses to process specific line numbers
- if option == '2':
- # Ask user for specific line numbers
- line_numbers = get_user_input_for_line_numbers()
- # Display selected lines with line numbers
- print("\nSelected lines:")
- for num in line_numbers:
- print(f"{num}. {all_lines[num - 1].strip()}")
- while True:
- # Display continue options
- print("\nContinue options:")
- display_options(["1. Continue with Encapsulation", "2. Go back: Edit line numbers", "3. Exit Program"])
- # Ask user if they want to continue, edit line numbers, or exit
- choice = input("Choose an option: ")
- if choice == '1':
- break
- elif choice == '2':
- line_numbers = get_user_input_for_line_numbers()
- print("\nSelected lines:")
- for num in line_numbers:
- print(f"{num}. {all_lines[num - 1].strip()}")
- elif choice == '3':
- sys.exit(0)
- else:
- print("Invalid choice. Exiting...")
- sys.exit(0)
- elif option == '1':
- print("All lines have been encapsulated.")
- else:
- print("Invalid option selected. Exiting...")
- sys.exit(0)
- # Open the output file in write mode
- with open("encapsulated_output.txt", "w") as file:
- # Iterate over each line in the all lines
- for i, line in enumerate(all_lines, 1):
- # Remove the newline character at the end of the line
- line = line.strip()
- if i in line_numbers:
- # Encapsulate the selected line with user-defined left and right encapsulation using string formatting
- modified_line = "{}{}{}".format(left_encapsulation, line, right_encapsulation)
- else:
- # Otherwise, keep the line as it is
- modified_line = line
- # Write the modified line to the output file
- file.write(modified_line + "\n")
- print("Output has been written to encapsulated_output.txt.")
- sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement