Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ def convert(
**kwargs: Any, # Options to pass to the converter
) -> DocumentConverterResult:
# Read the file content
data = file_stream.read()
if stream_info.charset:
content = file_stream.read().decode(stream_info.charset)
try:
content = data.decode(stream_info.charset)
except UnicodeDecodeError:
content = str(from_bytes(data).best())
else:
content = str(from_bytes(file_stream.read()).best())
content = str(from_bytes(data).best())

# Parse CSV content
reader = csv.reader(io.StringIO(content))
Expand Down
14 changes: 14 additions & 0 deletions packages/markitdown/tests/test_module_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,20 @@ def test_data_uris() -> None:
assert data == b"Hello, World!"


def test_csv_falls_back_when_charset_hint_fails() -> None:
content = ("x" * 4096 + ",café\n").encode("utf-8")
result = MarkItDown().convert(
io.BytesIO(content),
stream_info=StreamInfo(
extension=".csv",
mimetype="text/csv",
charset="ascii",
),
)

assert "café" in result.markdown


def test_file_uris() -> None:
# Test file URI with an empty host
file_uri = "file:///path/to/file.txt"
Expand Down