Add parse_datafile function
This commit is contained in:
1 parent
d773c7d3a9
commit
39c9426f71
1 file changed
+12
@@ -1,5 +1,17 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
def parse_datafile(data_file: Path, output_format: str) -> str | list:
|
||||||
|
with open(data_file, 'r', encoding="utf-8") as data_file:
|
||||||
|
match output_format:
|
||||||
|
case 'string':
|
||||||
|
return data_file.read().strip()
|
||||||
|
case 'list':
|
||||||
|
return data_file.read().strip().splitlines()
|
||||||
|
case 'int_list':
|
||||||
|
return [int(line) for line in data_file.read().strip().splitlines()]
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
def data_as_string(data_file: Path) -> str:
|
def data_as_string(data_file: Path) -> str:
|
||||||
with open(data_file) as file:
|
with open(data_file) as file:
|
||||||
return file.read().strip()
|
return file.read().strip()
|
||||||
|
|||||||
Reference in new issue
Block a user