Skip to content

Instantly share code, notes, and snippets.

@jin-zhe
Created November 26, 2024 07:11
Show Gist options
  • Save jin-zhe/a775c04f5d556a43e84705ea39ba20ad to your computer and use it in GitHub Desktop.
Save jin-zhe/a775c04f5d556a43e84705ea39ba20ad to your computer and use it in GitHub Desktop.
IO convenience functions for Python
def load_csv(csv_path: Path, ignore_first_row=True, ignore_empty_rows=True, delimiter=','):
'''
Returns all the rows of a csv file
'''
rows = []
with csv_path.open() as csvfile:
csv_reader = csv.reader(csvfile, delimiter=delimiter)
if ignore_first_row:
next(csv_reader)
for row in csv_reader:
if len(row) != 0 or not ignore_empty_rows:
rows.append(row)
return rows
def save_csv(rows, output_path: Path, titles=None, delimiter=','):
'''
Writes out rows to csv file given output path
'''
with output_path.open('w') as csvfile:
out_writer = csv.writer(csvfile, delimiter=delimiter)
if titles:
out_writer.writerow(titles)
for row in rows:
out_writer.writerow(row)
def load_json(json_path: Path):
'''
Loads JSON from given path
'''
with json_path.open() as f:
return json.load(f)
def save_json(obj, json_path: Path):
'''
Saves given object `obj` as JSON file
'''
with json_path.open('w') as f:
json.dump(obj, f)
def load_yaml(yaml_path: Path, loader='FullLoader'):
'''
Loads YAML from given path
Args:
yaml_path: (Path) path of yaml file
loader: (str) choice of yaml loader [SafeLoader | FullLoader | UnsafeLoader/Loader]
- SafeLoader: Recommended for untrusted input by loading a subset of YAML.
- FullLoader: For more trusted inputs with limitation prohibiting arbitrary code execution.
- UnsafeLoader/Loader: Unsafe but has the full power of YAML
'''
with yaml_path.open() as f:
return yaml.load(f, Loader=getattr(yaml, loader))
def save_yaml(obj, yaml_path: Path):
'''
Saves given object `obj` as YAML file
'''
with yaml_path.open('w') as f:
yaml.dump(obj, f)
def load_pickle(pkl_path: Path):
'''
Loads a pickle file from the given path
'''
with pkl_path.open('rb') as f:
return pickle.load(f)
def save_pickle(obj, pkl_path: Path):
'''
Saves a given pickle file to the given path
'''
with pkl_path.open('wb') as f:
pickle.dump(obj, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment