You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.1 KiB

  1. import abc
  2. import os
  3. import uuid
  4. import zipfile
  5. import pandas as pd
  6. def zip_files(files, dirname):
  7. save_file = os.path.join(dirname, f"{uuid.uuid4()}.zip")
  8. with zipfile.ZipFile(save_file, "w", compression=zipfile.ZIP_DEFLATED) as zf:
  9. for file in files:
  10. zf.write(filename=file, arcname=os.path.basename(file))
  11. return save_file
  12. class Writer(abc.ABC):
  13. extension = ""
  14. @staticmethod
  15. @abc.abstractmethod
  16. def write(file, dataset: pd.DataFrame):
  17. raise NotImplementedError("Please implement this method in the subclass.")
  18. class CsvWriter(Writer):
  19. extension = "csv"
  20. @staticmethod
  21. def write(file, dataset: pd.DataFrame):
  22. dataset.to_csv(file, index=False, encoding="utf-8")
  23. class JsonWriter(Writer):
  24. extension = "json"
  25. @staticmethod
  26. def write(file, dataset: pd.DataFrame):
  27. dataset.to_json(file, orient="records", force_ascii=False)
  28. class JsonlWriter(Writer):
  29. extension = "jsonl"
  30. @staticmethod
  31. def write(file, dataset: pd.DataFrame):
  32. dataset.to_json(file, orient="records", force_ascii=False, lines=True)