import re from datetime import datetime, timedelta # Function to convert the date format def convert_date_format(date_str): # Extract the date part using regex match = re.search(r'Date:\s*(\d{1,2})\s+(\w+)\s+(\d{4})\s+at\s+(\d{2}:\d{2}:\d{2})\s+GMT', date_str) if match: day = match.group(1) month = match.group(2) year = match.group(3) time = match.group(4) # Convert month name to month number month_number = datetime.strptime(month, '%B').month # Create a new datetime object dt = datetime(year=int(year), month=month_number, day=int(day), hour=int(time.split(':')[0]), minute=int(time.split(':')[1]), second=int(time.split(':')[2])) # Adjust the time if needed (e.g., to UTC+9) dt += timedelta(hours=21) # Format the new date string with AM/PM and "+++" return f"[{dt.strftime('%Y-%m-%d %I:%M:%S %p')}] +++" return date_str # Return original if no match # Read the input file and write to the output file input_file = 'input.txt' # Change to your input file name output_file = 'output.txt' # Change to your desired output file name with open(input_file, 'r') as file: content = file.readlines() with open(output_file, 'w') as file: for i, line in enumerate(content): # Write the formatted date line if it matches the format if 'Date:' in line: converted_line = convert_date_format(line) file.write(converted_line + '\n') # Write the converted date line first file.write(line) # Write the original date line # Check if there is a next line and write it (the original text line) if i + 1 < len(content): file.write(content[i + 1]) # Write the original text line else: # Write other lines unchanged file.write(line)