From 9349bf3bbffc1202e2675278c5fcf7cfb24364cc Mon Sep 17 00:00:00 2001 From: David Minton Date: Sat, 4 Sep 2021 17:09:20 -0400 Subject: [PATCH] Fixed how character strings are stripped and encoded --- python/swiftest/swiftest/io.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/python/swiftest/swiftest/io.py b/python/swiftest/swiftest/io.py index a82c5f043..cae553b13 100644 --- a/python/swiftest/swiftest/io.py +++ b/python/swiftest/swiftest/io.py @@ -704,6 +704,9 @@ def swiftest2xr(param): return ds +def xstrip(a): + func = lambda x: np.char.strip(x) + return xr.apply_ufunc(func, a.str.decode(encoding='utf-8')) def clean_string_values(param, ds): """ @@ -719,13 +722,13 @@ def clean_string_values(param, ds): ds : xarray dataset with the strings cleaned up """ if 'name' in ds: - ds['name'] = ds['name'].str.decode(encoding='utf-8') + ds['name'] = xstrip(ds['name']) if 'particle_type' in ds: - ds['particle_type'] = ds['particle_type'].str.decode(encoding='utf-8') + ds['particle_type'] = xstrip(ds['particle_type']) if 'status' in ds: - ds['status'] = ds['status'].str.decode(encoding='utf-8') + ds['status'] = xstrip(ds['status']) if 'origin_type' in ds: - ds['origin_type'] = ds['origin_type'].str.decode(encoding='utf-8') + ds['origin_type'] = xstrip(ds['origin_type']) return ds