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):
  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. data = json.load(f, object_pairs_hook=OrderedDict)
  26. return data
  27. def save_json(filename, data, encoding='utf-8', sort_keys=False):
  28. if not filename.endswith('.json'):
  29. filename = filename + '.json'
  30. with open(filename, 'w', encoding=encoding) as f:
  31. json.dump(data, f, indent=4, separators=(',',': '),
  32. ensure_ascii=False, sort_keys=sort_keys)