Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Filename: cut_before_markers.py
- # Version: 1.00
- # Author: Jeoi Reqi
- """
- This script cuts text before specified markers in each line of a text file.
- Requirements:
- - Python 3.x
- Usage:
- 1. Ensure the script 'cut_before_markers.py' is in the same directory as the input text file.
- 2. Open the script in a text editor and edit the value of 'file_path' variable to point to the input text file.
- 3. Run the script using a Python interpreter.
- 4. The script will cut text before specified markers in each line of the input file and save the modified text back to the same file.
- 5. Review the console output to confirm the successful execution of the script.
- """
- def remove_before_marker(line, marker):
- """
- Remove text before the specified marker in a line.
- Args:
- line (str): The input line.
- marker (str): The marker to cut the line before.
- Returns:
- str: The modified line without text before the marker.
- """
- index = line.find(marker)
- if index != -1:
- return line[index + len(marker):] + '\n'
- return line
- file_path = 'input_file.txt' # Path to the input file
- output_lines = [] # List to store modified lines
- try:
- with open(file_path, 'r') as file:
- for line in file:
- # Remove text before the specified marker in each line
- modified_line = remove_before_marker(line, " : ") # Edit this to your set marker
- output_lines.append(modified_line)
- with open(file_path, 'w') as file:
- # Write modified lines back to the input file
- file.writelines(output_lines)
- print("Characters before '**' marker have been removed from 'input_file.txt'.")
- except Exception as e:
- print("An error occurred:", e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement