Skip to content

bug/JSON and CSV detection for text/plain MIME types fails with non UTF-8 encodings #705

Closed

Description

@MthwRobinson

Describe the bug
JSON and CSV detection for text/plain MIME types fails with non UTF-8 encodings

To Reproduce

In [9]: _is_text_file_a_csv(filename="example-docs/fake-text-utf-32.txt")
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
Cell In[9], line 1
----> 1 _is_text_file_a_csv(filename="example-docs/fake-text-utf-32.txt")

File ~/repos/forks/coniferish-unstructured/unstructured/file_utils/filetype.py:364, in _is_text_file_a_csv(filename, file)
    359 def _is_text_file_a_csv(
    360     filename: Optional[str] = None,
    361     file: Optional[IO] = None,
    362 ):
    363     """Detects if a file that has a text/plain MIME type is a CSV file."""
--> 364     file_text = _read_file_start_for_type_check(file=file, filename=filename)
    365     lines = file_text.strip().splitlines()
    366     if len(lines) < 2:

File ~/repos/forks/coniferish-unstructured/unstructured/file_utils/filetype.py:346, in _read_file_start_for_type_check(filename, file)
    344 if filename is not None:
    345     with open(filename) as f:
--> 346         file_text = f.read(4096)
    347 return file_text

File ~/.pyenv/versions/3.8.13/lib/python3.8/codecs.py:322, in BufferedIncrementalDecoder.decode(self, input, final)
    319 def decode(self, input, final=False):
    320     # decode input (taking the buffer into account)
    321     data = self.buffer + input
--> 322     (result, consumed) = self._buffer_decode(data, self.errors, final)
    323     # keep undecoded input until the next call
    324     self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Expected behavior
This code should work with non utf-8 encodings

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • unstructured==0.7.2

Activity

  1. added
    bug
    on Jun 8, 2023
  2. cragwolfe commented on Jun 9, 2023

    @cragwolfe
    Contributor
  3. self-assigned this
    on Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions