Skip to content

Commit 5ad260c

Browse files
jswhit2jswhit2
authored andcommitted
change default encoding for stringtochar, chartostring to ascii for dtype='S' (issue #1464)
1 parent a332de8 commit 5ad260c

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

examples/tutorial.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def walktree(top):
163163
datac2.imag = datain['imag']
164164
print(datac.dtype,datac)
165165
print(datac2.dtype,datac2)
166+
nc.close()
166167

167168
# more complex compound type example.
168169
nc = Dataset('compound_example.nc','w') # create a new dataset.

src/netCDF4/_netCDF4.pyx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6788,7 +6788,7 @@ returns a rank 1 numpy character array of length NUMCHARS with datatype `'S1'`
67886788
arr[0:len(string)] = tuple(string)
67896789
return arr
67906790

6791-
def stringtochar(a,encoding='utf-8',n_strlen=None):
6791+
def stringtochar(a,encoding=None,n_strlen=None):
67926792
"""
67936793
**`stringtochar(a,encoding='utf-8',n_strlen=None)`**
67946794
@@ -6809,10 +6809,15 @@ used to represent each string in the input array).
68096809
returns a numpy character array with datatype `'S1'` or `'U1'`
68106810
and shape `a.shape + (N,)`, where N is the length of each string in a."""
68116811
dtype = a.dtype.kind
6812-
if n_strlen is None:
6813-
n_strlen = a.dtype.itemsize
68146812
if dtype not in ["S","U"]:
68156813
raise ValueError("type must string or unicode ('S' or 'U')")
6814+
if encoding is None:
6815+
if dtype == 'S':
6816+
encoding = 'ascii'
6817+
else:
6818+
encoding = 'utf-8'
6819+
if n_strlen is None:
6820+
n_strlen = a.dtype.itemsize
68166821
if encoding in ['none','None','bytes']:
68176822
b = numpy.array(tuple(a.tobytes()),'S1')
68186823
elif encoding == 'ascii':
@@ -6827,7 +6832,7 @@ and shape `a.shape + (N,)`, where N is the length of each string in a."""
68276832
b = numpy.array([[bb[i:i+1] for i in range(n_strlen)] for bb in bbytes])
68286833
return b
68296834

6830-
def chartostring(b,encoding='utf-8'):
6835+
def chartostring(b,encoding=None):
68316836
"""
68326837
**`chartostring(b,encoding='utf-8')`**
68336838
@@ -6846,6 +6851,11 @@ returns a numpy string array with datatype `'UN'` (or `'SN'`) and shape
68466851
dtype = b.dtype.kind
68476852
if dtype not in ["S","U"]:
68486853
raise ValueError("type must be string or unicode ('S' or 'U')")
6854+
if encoding is None:
6855+
if dtype == 'S':
6856+
encoding = 'ascii'
6857+
else:
6858+
encoding = 'utf-8'
68496859
bs = b.tobytes()
68506860
slen = int(b.shape[-1])
68516861
if encoding in ['none','None','bytes']:

0 commit comments

Comments
 (0)