gebos_convert.py 1001 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import csv
  2. import re
  3. from pathlib import Path
  4. def convert_dir(path):
  5. source_path = Path(path)
  6. target_path = source_path.parent.joinpath('staging')
  7. for source_file in source_path.glob('*.csv'):
  8. print(source_file.name)
  9. target_file = target_path / source_file.name
  10. convert_file(source_file, target_file)
  11. def convert_file(source, target):
  12. with open(source, 'r', encoding='utf-8', newline='') as frh:
  13. with open(target, 'w', encoding='latin-1', newline='') as fwh:
  14. csv_reader = csv.reader(frh, delimiter=';')
  15. csv_writer = csv.writer(fwh, delimiter='\t')
  16. for row in csv_reader:
  17. converted = [convert_field(col) for col in row]
  18. csv_writer.writerow(converted)
  19. def convert_field(col):
  20. if re.match(r'\d:\d\d:\d\d', col):
  21. col = '0' + col
  22. return col
  23. def main():
  24. convert_dir('E:\\GEBOS\\data')
  25. if __name__ == '__main__':
  26. main()