|
| 1 | +import os |
1 | 2 | from abc import ABC, abstractmethod
|
2 | 3 | from io import StringIO
|
3 | 4 | from typing import Dict, List, Optional, Tuple
|
@@ -71,11 +72,21 @@ def tsv_parser(
|
71 | 72 | raise ValueError(f"Invalid input: {e}")
|
72 | 73 |
|
73 | 74 |
|
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.""" |
75 | 77 | with open(path, "r") as handle:
|
76 | 78 | return tsv_parser(handle.read(), mapping, columns, header)
|
77 | 79 |
|
78 | 80 |
|
| 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 | + |
79 | 90 | def read_from_tsv(
|
80 | 91 | notesPath: Optional[str],
|
81 | 92 | ratingsPath: Optional[str],
|
|
0 commit comments