import re def extract_customer_ids(file_path): # Read the file content with open(file_path, "r") as file: content = file.read() # Define regex pattern for Stripe customer IDs # Matches 'cus_' followed by exactly 14 alphanumeric characters pattern = r"cus_[A-Za-z0-9]{14}" # Find all matches in the content customer_ids = re.findall(pattern, content) return customer_ids # Example usage: if __name__ == "__main__": file_path = "mozart_50k_stripe_customers_poorly_formatted.txt" customer_ids = extract_customer_ids(file_path) # Write customer IDs to output file output_file = "mozart_stripe_customer_ids.txt" with open(output_file, "w") as f: for customer_id in customer_ids: f.write(f"{customer_id}\n") print(f"Wrote {len(customer_ids)} customer IDs to {output_file}")