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.

44 lines
985 B

  1. import abc
  2. import pandas as pd
  3. class Writer(abc.ABC):
  4. extension = ""
  5. @staticmethod
  6. @abc.abstractmethod
  7. def write(file, dataset: pd.DataFrame):
  8. raise NotImplementedError("Please implement this method in the subclass.")
  9. class CsvWriter(Writer):
  10. extension = "csv"
  11. @staticmethod
  12. def write(file, dataset: pd.DataFrame):
  13. dataset.to_csv(file, index=False, encoding="utf-8")
  14. class JsonWriter(Writer):
  15. extension = "json"
  16. @staticmethod
  17. def write(file, dataset: pd.DataFrame):
  18. dataset.to_json(file, orient="records", force_ascii=False)
  19. class JsonlWriter(Writer):
  20. extension = "jsonl"
  21. @staticmethod
  22. def write(file, dataset: pd.DataFrame):
  23. dataset.to_json(file, orient="records", force_ascii=False, lines=True)
  24. class FastTextWriter(Writer):
  25. extension = "txt"
  26. @staticmethod
  27. def write(file, dataset: pd.DataFrame):
  28. dataset.to_csv(file, index=False, encoding="utf-8", header=False)