Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: extract_quoted_text.py
- # Author: Jeoi Reqi
- """
- This Python script extracts text enclosed in double quotes from a specified input file.
- It reads the content of the input file, searches for text surrounded by double quotes using regex,
- and writes the extracted text to a specified output file.
- Usage:
- - Place the script in the same directory as the input file.
- - Update 'input_filename' and 'output_filename' variables with the appropriate file names.
- - Run the script to extract and save the quoted text.
- Requirements:
- - This script requires Python 3.
- """
- def extract_quoted_text(input_file, output_file):
- try:
- with open(input_file, 'r', encoding='utf-8') as input_file:
- content = input_file.read()
- # Extract text enclosed in double quotes using regex
- import re
- quoted_text = re.findall(r'"([^"]*)"', content)
- # Write the extracted text to the output file
- with open(output_file, 'w', encoding='utf-8') as output_file:
- for text in quoted_text:
- output_file.write(text + '\n')
- print(f'Successfully extracted quoted text from {input_file.name} to {output_file.name}.')
- except Exception as e:
- print(f'An error occurred: {e}')
- # Example usage
- input_filename = 'input.txt' # Replace 'input.txt' with your input filename
- output_filename = 'output.txt' # Replace 'output.txt' with your output filename
- extract_quoted_text(input_filename, output_filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement