Skip to content

Commit 6729c43

Browse files
committed
FEAT: added adapter for .tsv files
1 parent a68a15e commit 6729c43

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

larray_editor/arrayadapter.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,14 +2918,16 @@ def open(cls, fpath):
29182918

29192919

29202920
class CsvFileAdapter(TextFileAdapter):
2921+
DELIMITER = ','
2922+
29212923
def __init__(self, data, attributes):
29222924
# we know the module is loaded but it is not in the current namespace
29232925
csv = sys.modules['csv']
29242926
TextFileAdapter.__init__(self, data=data, attributes=attributes)
29252927
if self._nbytes > 0:
29262928
first_line = self._get_lines(0, 1)
29272929
assert len(first_line) == 1
2928-
reader = csv.reader([first_line[0]])
2930+
reader = csv.reader([first_line[0]], delimiter=self.DELIMITER)
29292931
self._colnames = next(reader)
29302932
else:
29312933
self._colnames = []
@@ -2952,7 +2954,7 @@ def get_values(self, h_start, v_start, h_stop, v_stop):
29522954
# we know the module is loaded but it is not in the current namespace
29532955
csv = sys.modules['csv']
29542956
# Note that csv reader actually needs a line-based input
2955-
reader = csv.reader(lines)
2957+
reader = csv.reader(lines, delimiter=self.DELIMITER)
29562958
return [line[h_start:h_stop] for line in reader]
29572959

29582960

@@ -2963,6 +2965,17 @@ def open(cls, fpath):
29632965
return open(fpath, 'rt')
29642966

29652967

2968+
class TsvFileAdapter(CsvFileAdapter):
2969+
DELIMITER = '\t'
2970+
2971+
2972+
@path_adapter_for('.tsv', 'csv')
2973+
class TsvPathAdapter(TsvFileAdapter):
2974+
@classmethod
2975+
def open(cls, fpath):
2976+
return open(fpath, 'rt')
2977+
2978+
29662979
# This is a Path adapter (it handles Path objects) because pyreadstat has no
29672980
# object representing open files
29682981
@path_adapter_for('.sas7bdat', 'pyreadstat')

0 commit comments

Comments
 (0)