utils_file_io.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import json
  2. from collections import OrderedDict
  3. def load_list(filename, encoding='utf-8', start=0, stop=None):
  4. assert isinstance(start, int) and start >= 0
  5. assert (stop is None) or (isinstance(stop, int) and stop > start)
  6. lines = []
  7. with open(filename, 'r', encoding=encoding) as f:
  8. for _ in range(start):
  9. f.readline()
  10. for k, line in enumerate(f):
  11. if (stop is not None) and (k + start > stop):
  12. break
  13. lines.append(line.rstrip('\n'))
  14. return lines
  15. def save_list(filename, list_obj, encoding='utf-8', append_break=True):
  16. with open(filename, 'w', encoding=encoding) as f:
  17. if append_break:
  18. for item in list_obj:
  19. f.write(str(item) + '\n')
  20. else:
  21. for item in list_obj:
  22. f.write(str(item))
  23. def load_json(filename, encoding='utf-8'):
  24. with open(filename, 'r', encoding=encoding) as f:
  25. dict_obj = json.load(f, object_pairs_hook=OrderedDict)
  26. return dict_obj
  27. def save_json(filename, dict_obj, encoding='utf-8', sort_keys=False):
  28. with open(filename, 'w', encoding=encoding) as f:
  29. json.dump(dict_obj, f, indent=4, separators=(',',': '),
  30. ensure_ascii=False, sort_keys=sort_keys)