Skip to content

Commit

Permalink
Use fixed-width, null-padded strings
Browse files Browse the repository at this point in the history
  • Loading branch information
mobiusklein committed Nov 14, 2023
1 parent 43b52d7 commit 9169b19
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion psims/mzmlb/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,14 @@ def create_buffer(self, name, content):

def _create_fixed_length_attribute(self, group: h5py.Group, name: str, value: str, encoding: str='ascii'):
encoded = value.encode(encoding)
group.attrs.create(name, encoded, dtype=h5py.string_dtype('ascii', len(encoded)))
type_id = h5py.h5t.TypeID.copy(h5py.h5t.C_S1)
type_id.set_size(len(value) + 1)
type_id.set_strpad(h5py.h5t.STR_NULLTERM)
if encoding == 'ascii':
type_id.set_cset(h5py.h5t.CSET_ASCII)
elif encoding == 'utf8':
type_id.set_cset(h5py.h5t.CSET_UTF8)
else:
warnings.warn(f"{encoding} is not compatible with HDF5, defaulting to UTF8")
type_id.set_cset(h5py.h5t.CSET_UTF8)
group.attrs.create(name, encoded, dtype=type_id)

0 comments on commit 9169b19

Please sign in to comment.