Skip to content

Commit d3e2c98

Browse files
authored
Merge pull request #175 from avalanchesiqi/main
Reading multiple rating TSV files from a folder
2 parents 9ee120f + cc0e738 commit d3e2c98

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

sourcecode/scoring/process_data.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from abc import ABC, abstractmethod
23
from io import StringIO
34
from typing import Dict, List, Optional, Tuple
@@ -71,11 +72,21 @@ def tsv_parser(
7172
raise ValueError(f"Invalid input: {e}")
7273

7374

74-
def tsv_reader(path: str, mapping, columns, header=False):
75+
def tsv_reader_single(path: str, mapping, columns, header=False, parser=tsv_parser):
76+
"""Read a single TSV file."""
7577
with open(path, "r") as handle:
7678
return tsv_parser(handle.read(), mapping, columns, header)
7779

7880

81+
def tsv_reader(path: str, mapping, columns, header=False, parser=tsv_parser):
82+
"""Read a single TSV file or a directory of TSV files."""
83+
if os.path.isdir(path):
84+
dfs = [tsv_reader_single(os.path.join(path, filename), mapping, columns, header, parser) for filename in os.listdir(path) if filename.endswith(".tsv")]
85+
return pd.concat(dfs)
86+
else:
87+
return tsv_reader_single(path, mapping, columns, header, parser)
88+
89+
7990
def read_from_tsv(
8091
notesPath: Optional[str],
8192
ratingsPath: Optional[str],

0 commit comments

Comments
 (0)