Skip to content

Commit 100ff64

Browse files
committed
use chardet to guess the encoding
1 parent b34d835 commit 100ff64

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

ctd/read.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from io import StringIO
1414
from pathlib import Path
1515

16+
import chardet
1617
import gsw
1718
import numpy as np
1819
import pandas as pd
@@ -50,9 +51,7 @@ def _open_compressed(fname):
5051
cfile = zfile.open(name)
5152
else:
5253
raise ValueError(
53-
"Unrecognized file extension. Expected .gzip, .bz2, or .zip, got {}".format(
54-
extension,
55-
),
54+
f"Unrecognized file extension. Expected .gzip, .bz2, or .zip, got {extension}",
5655
)
5756
contents = cfile.read()
5857
cfile.close()
@@ -74,7 +73,8 @@ def _read_file(fname):
7473
f"Unrecognized file extension. Expected .cnv, .edf, .txt, .ros, or .btl got {extension}",
7574
)
7675
# Read as bytes but we need to return strings for the parsers.
77-
text = contents.decode(encoding="utf-8", errors="replace")
76+
encoding = chardet.detect(contents)["encoding"]
77+
text = contents.decode(encoding=encoding, errors="replace")
7878
return StringIO(text)
7979

8080

0 commit comments

Comments
 (0)