Skip to content

Commit 2f8219a

Browse files
authored
Merge pull request nexpy#261 from rayosborn:improve-encoding-detection
Improve-encoding-detection
2 parents 8c35a56 + 8970bce commit 2f8219a

12 files changed

Lines changed: 62 additions & 15 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ classifiers = [
2727
keywords = ["data format", "HDF5", "neutron scattering", "x-ray scattering"]
2828
requires-python = ">=3.9"
2929
dependencies = [
30+
"chardet",
3031
"colored",
3132
"h5py",
3233
"hdf5plugin",

src/nexusformat/nexus/completer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -----------------------------------------------------------------------------
2-
# Copyright (c) 2019-2025, NeXpy Development Team.
2+
# Copyright (c) 2019-2026, NeXpy Development Team.
33
#
44
# Author: Paul Kienzle, Ray Osborn
55
#

src/nexusformat/nexus/lock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -----------------------------------------------------------------------------
2-
# Copyright (c) 2019-2022, NeXpy Development Team.
2+
# Copyright (c) 2019-2026, NeXpy Development Team.
33
#
44
# Author: Paul Kienzle, Ray Osborn
55
#

src/nexusformat/nexus/tree.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,17 @@ def text(value):
266266
if isinstance(value, bytes):
267267
try:
268268
_text = value.decode(NX_CONFIG['encoding'])
269-
except UnicodeDecodeError:
270-
if NX_CONFIG['encoding'] == 'utf-8':
271-
_text = value.decode('latin-1')
272-
else:
273-
_text = value.decode('utf-8')
269+
except (UnicodeDecodeError, KeyError, LookupError):
270+
import chardet
271+
detected = chardet.detect(value)
272+
encoding = detected['encoding']
273+
if not encoding:
274+
encoding = 'latin-1'
275+
_text = value.decode(encoding, errors='replace')
274276
else:
275277
_text = str(value)
276278
return _text.replace('\x00', '').rstrip()
277279

278-
279280
def is_text(value):
280281
"""
281282
Return True if the value represents text.

src/nexusformat/scripts/nexusformat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# -----------------------------------------------------------------------------
3-
# Copyright (c) 2019-2021, NeXpy Development Team.
3+
# Copyright (c) 2019-2026, NeXpy Development Team.
44
#
55
# Distributed under the terms of the Modified BSD License.
66
#

src/nexusformat/scripts/nxcheck.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# -----------------------------------------------------------------------------
3-
# Copyright (c) 2025, NeXpy Development Team.
3+
# Copyright (c) 2025-2026, NeXpy Development Team.
44
#
55
# Distributed under the terms of the Modified BSD License.
66
#

src/nexusformat/scripts/nxconsolidate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# -----------------------------------------------------------------------------
3-
# Copyright (c) 2022, NeXpy Development Team.
3+
# Copyright (c) 2022-2026, NeXpy Development Team.
44
#
55
# Distributed under the terms of the Modified BSD License.
66
#

src/nexusformat/scripts/nxdir.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# -----------------------------------------------------------------------------
3-
# Copyright (c) 2019-2021, NeXpy Development Team.
3+
# Copyright (c) 2019-2026, NeXpy Development Team.
44
#
55
# Distributed under the terms of the Modified BSD License.
66
#

src/nexusformat/scripts/nxduplicate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# -----------------------------------------------------------------------------
3-
# Copyright (c) 2019-2021, NeXpy Development Team.
3+
# Copyright (c) 2019-2026, NeXpy Development Team.
44
#
55
# Distributed under the terms of the Modified BSD License.
66
#

src/nexusformat/scripts/nxinspect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# -----------------------------------------------------------------------------
3-
# Copyright (c) 2025, NeXpy Development Team.
3+
# Copyright (c) 2025-2026, NeXpy Development Team.
44
#
55
# Distributed under the terms of the Modified BSD License.
66
#

0 commit comments

Comments
 (0)