diff options
author | Frederick Muriuki Muriithi | 2025-06-02 12:19:20 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2025-06-02 12:19:20 -0500 |
commit | 6870c08d484f482cc2f2501d28b474636dd0810d (patch) | |
tree | e61cf114cbda7b2c7284d65558ceac8de1ec9ec8 | |
parent | f656711fa9b9ac86f320a4d88137779703df8d9b (diff) | |
download | gn-uploader-6870c08d484f482cc2f2501d28b474636dd0810d.tar.gz |
Convert N/A values to NoneType objects.
-rw-r--r-- | r_qtl/r_qtl2.py | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/r_qtl/r_qtl2.py b/r_qtl/r_qtl2.py index b1eae90..ec70c5c 100644 --- a/r_qtl/r_qtl2.py +++ b/r_qtl/r_qtl2.py @@ -6,7 +6,7 @@ import json from pathlib import Path from functools import reduce, partial from zipfile import ZipFile, is_zipfile -from typing import Union, Iterator, Iterable, Callable, Optional +from typing import Union, Iterator, Iterable, Callable, Optional, Sequence import yaml @@ -572,14 +572,21 @@ def read_text_file(filepath: Union[str, Path]) -> Iterator[str]: yield line -def read_csv_file(filepath: Union[str, Path], - separator: str = ",", - comment_char: str = "#") -> Iterator[tuple[str, ...]]: +def read_csv_file( + filepath: Union[str, Path], + separator: str = ",", + comment_char: str = "#", + na_strings: Sequence[str] = ["NA"] +) -> Iterator[tuple[str, ...]]: """Read a file as a csv file.""" + def __none_if_na__(val): + return None if val in na_strings else val + for line in read_text_file(filepath): if line.startswith(comment_char): continue - yield tuple(field.strip() for field in line.split(separator)) + yield tuple(__none_if_na__(field.strip()) + for field in line.split(separator)) def read_csv_file_headers( |