utils_file_io.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import json
  2. import base64
  3. import numbers
  4. from collections import OrderedDict
  5. def load_list(filename, encoding='utf-8', start=0, stop=None):
  6. assert isinstance(start, numbers.Integral) and start >= 0
  7. assert (stop is None) or (isinstance(stop, numbers.Integral) and stop > start)
  8. lines = []
  9. with open(filename, 'r', encoding=encoding) as f:
  10. for _ in range(start):
  11. f.readline()
  12. for k, line in enumerate(f):
  13. if (stop is not None) and (k + start > stop):
  14. break
  15. lines.append(line.rstrip('\n'))
  16. return lines
  17. def save_list(filename, list_obj, encoding='utf-8', append_break=True):
  18. with open(filename, 'w', encoding=encoding) as f:
  19. if append_break:
  20. for item in list_obj:
  21. f.write(str(item) + '\n')
  22. else:
  23. for item in list_obj:
  24. f.write(str(item))
  25. def load_json(filename, encoding='utf-8'):
  26. with open(filename, 'r', encoding=encoding) as f:
  27. data = json.load(f, object_pairs_hook=OrderedDict)
  28. return data
  29. def save_json(filename, data, encoding='utf-8', cls=None, sort_keys=False):
  30. if not filename.endswith('.json'):
  31. filename = filename + '.json'
  32. with open(filename, 'w', encoding=encoding) as f:
  33. json.dump(data, f, indent=4, separators=(',',': '),
  34. ensure_ascii=False, cls=cls, sort_keys=sort_keys)
  35. def load_as_base64(filename: str) -> str:
  36. with open(filename, 'rb') as f:
  37. raw_bytes = f.read()
  38. b64_bytes = base64.b64encode(raw_bytes)
  39. b64_str = b64_bytes.decode()
  40. return b64_str