Coverage for datakiln/io.py: 60%
20 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-16 23:23 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-16 23:23 +0000
1"""
2Helpers to read multiple data file extensions.
3"""
4import warnings
5from io import BytesIO, TextIOWrapper, IOBase # pylint: disable=import-self
7import pandas as pd
10class NullStripper(IOBase):
11 """
12 Remove NULL characters
13 """
14 def __init__(self, target):
15 self.target = target
17 def read(self, size=-1):
18 """
19 Read
20 """
21 # Read a chunk from the original buffer (e.g., StringIO)
22 chunk = self.target.read(size)
23 if chunk is None:
24 return None
26 # Remove the null bytes from the chunk and return it
27 return chunk.replace('\x00', '')
29 def readable(self):
30 """
31 Return whether object was opened for reading.
33 If False, read() will raise OSError.
34 """
35 return True
38def open_excel(path, encoding):
39 """
40 Excel file reader.
42 :param data_path: Path to file.
43 :type data_path: str
45 :param encoding: encoding of the data file.
46 :type encoding: str
48 :return: File buffer.
49 :rtype: TextIOWrapper
50 """
51 buffer = TextIOWrapper(BytesIO(), encoding=encoding)
52 with warnings.catch_warnings(record=True):
53 warnings.simplefilter("always")
54 buffer.writelines(
55 pd.read_excel(path, dtype=object).to_csv(index=False))
56 buffer.seek(0)
57 return buffer