1. import re
  2. import csv
  3. def transform_file(input_filepath, output_filepath, transform_function):
  4. """
  5. Transforms data in a file using a provided function.
  6. Args:
  7. input_filepath (str): Path to the input file.
  8. output_filepath (str): Path to the output file.
  9. transform_function (callable): Function to apply to each line of data.
  10. Takes a single line (str) as input and returns the transformed line (str).
  11. """
  12. try:
  13. with open(input_filepath, 'r', encoding='utf-8') as infile, \
  14. open(output_filepath, 'w', encoding='utf-8') as outfile:
  15. # Iterate through each line in the input file
  16. for line in infile:
  17. # Apply the transformation function to the current line
  18. transformed_line = transform_function(line)
  19. # Write the transformed line to the output file
  20. outfile.write(transformed_line)
  21. except FileNotFoundError:
  22. print(f"Error: Input file not found at {input_filepath}")
  23. except Exception as e:
  24. print(f"An error occurred: {e}")
  25. def clean_data(line):
  26. """
  27. Removes leading/trailing whitespace from a line.
  28. """
  29. return line.strip()
  30. def convert_to_uppercase(line):
  31. """
  32. Converts a line to uppercase.
  33. """
  34. return line.upper()
  35. def replace_pattern(line, pattern, replacement):
  36. """
  37. Replaces all occurrences of a pattern in a line with a replacement string.
  38. """
  39. return re.sub(pattern, replacement, line)
  40. def extract_field(line, field_regex):
  41. """
  42. Extracts a specific field from a line using a regular expression.
  43. Returns the extracted field or None if not found.
  44. """
  45. match = re.search(field_regex, line)
  46. if match:
  47. return match.group(1) # Return the captured group
  48. else:
  49. return None
  50. def split_and_combine(line, delimiter, combine_char):
  51. """
  52. Splits a line based on a delimiter and combines the resulting parts with a character.
  53. """
  54. parts = line.split(delimiter)
  55. return combine_char.join(parts)
  56. if __name__ == '__main__':
  57. # Example Usage:
  58. # 1. Clean data and save to a new file
  59. transform_file("input.txt", "output_cleaned.txt", clean_data)
  60. # 2. Convert to uppercase
  61. transform_file("input.txt", "output_uppercase.txt", convert_to_uppercase)
  62. # 3. Replace a pattern
  63. transform_file("input.txt", "output_replaced.txt", lambda line: replace_pattern(line, r"old_pattern", "new_string"))
  64. # 4. Extract a field using regex
  65. transform_file("input.txt", "output_extracted.txt", lambda line: extract_field(line, r"(\d+)\s+name"))
  66. # 5. Split and combine
  67. transform_file("input.txt", "output_combined.txt", lambda line: split_and_combine(line, ",", "-"))

Add your comment