Coverage for datakiln/io.py: 60%

20 statements  

« 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 

6 

7import pandas as pd 

8 

9 

10class NullStripper(IOBase): 

11 """ 

12 Remove NULL characters 

13 """ 

14 def __init__(self, target): 

15 self.target = target 

16 

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 

25 

26 # Remove the null bytes from the chunk and return it 

27 return chunk.replace('\x00', '') 

28 

29 def readable(self): 

30 """ 

31 Return whether object was opened for reading. 

32 

33 If False, read() will raise OSError. 

34 """ 

35 return True 

36 

37 

38def open_excel(path, encoding): 

39 """ 

40 Excel file reader. 

41 

42 :param data_path: Path to file. 

43 :type data_path: str 

44 

45 :param encoding: encoding of the data file. 

46 :type encoding: str 

47 

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