Hello all,
I’ve got the file dates_names.txt with the text:
Sabrina Garcia 20 October 1988
Winifred Wood 27 July 1988
Juan Kennedy 4 March 1988
Nina Beck 7 May 1988
Tanya Marshall 22 May 1988
Kelly Gardner 16 August 1988
Cristina Ortega 13 January 1988
Guy Carr 21 June 1988
Geneva Martinez 5 September 1988
Ricardo Howell 23 December 1988
Bernadette Rios 19 July 1988
What I want to do is extract the names into a file names.txt and another dates.txt
I already got the part to extract the dates to file dates.txt
but I can’t see how to approach the other bit which is, extract only the names to to a file names.txt.
this is the messy code
import re
def extract_dates_from_file(input_filename, output_filename):
"""
Extracts dates from an input file and writes them to an output file.
"""
dates = []
date_pattern = r"\b(\d{1,2})\s(January|February|March|April|May|June|July|August|September|October|November|December)\s(\d{4})\b"
try:
with open(input_file_name, 'r') as infile:
for line in infile:
match = re.search(date_pattern, line, re.IGNORECASE)
if match:
dates.append(match.group(0))
except FileNotFoundError:
print(f"Error: Input file '{input_filename}' not found.")
return
try:
with open(output_filename, 'w') as outfile:
outfile.write("[\n")
for i, date in enumerate(dates):
outfile.write(f" \"{date}\"")
if i < len(dates) - 1:
outfile.write(",")
outfile.write("\n")
outfile.write("]")
print(f"Dates extracted and written to '{output_filename}'.")
except Exception as e:
print(f"An error occurred writing to the output file: {e}")
# start names extract
def extract_names_from_file(input_filename, output_file_names):
names = []
names_str = "" #see if needs to check format
try:
with open(input_file_name, 'r') as infile:
for line in infile:
match = re.search(names_str, line, re.IGNORECASE)
if match:
names.append(match.group(0))
except FileNotFoundError:
print(f"Error: input file '{input_filename}' not found")
return
try:
with open(output_file_names, 'w') as outfile:
outfile.write("[\n")
for i, name in enumerate(names):
outfile.write(f" \{name}\"")
if i <len(names) - 1:
outfile.write(",")
outfile.write("\n")
outfile.write("]")
print(f"names extracted and written to '{output_file_names}'.")
except Exception as b:
print(f"error writing file '{b}' not found.")
# Example usage:
# read file READ.TXT
input_file_name = "D:/test-folder/python_test/read.txt" # Now a variable
# create and write in file DATES.txt
output_file_dates = "D:/test-folder/python_test/dates.txt" # Now a variable
#create and write file NAMES.txt
output_file_names = "D:/test-folder/python_test/names.txt"
extract_dates_from_file(input_file_name, output_file_dates)
extract_names_from_file(input_file_name, output_file_names)```