utils_file_io.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import json
  2. from collections import OrderedDict
  3. def load_list(filename, encoding='utf-8', start=0, stop=None, step=1):
  4. assert isinstance(start, int) and start >= 0
  5. assert (stop is None) or (isinstance(stop, int) and stop > start)
  6. assert isinstance(step, int) and step >= 1
  7. lines = []
  8. with open(filename, 'r', encoding=encoding) as f:
  9. for _ in range(start):
  10. f.readline()
  11. for k, line in enumerate(f):
  12. if (stop is not None) and (k + start > stop):
  13. break
  14. if k % step == 0:
  15. lines.append(line.rstrip())
  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_dict(filename, encoding='utf-8'):
  26. with open(filename, 'r', encoding=encoding) as f:
  27. dict_obj = json.load(f, object_pairs_hook=OrderedDict)
  28. return dict_obj
  29. def save_dict(filename, dict_obj, encoding='utf-8', sort=False):
  30. with open(filename, 'w', encoding=encoding) as f:
  31. json.dump(dict_obj, f, indent=4, separators=(',',': '),
  32. ensure_ascii=False, sort_keys=sort)