from netCDF4 import Dataset, stringtochar, chartostring
import random, numpy

# test utilities for converting arrays of fixed-length strings
# to arrays of characters (with an extra dimension), and vice-versa.

FILE_NAME = 'tst_stringarr.nc'
FILE_FORMAT = 'NETCDF4_CLASSIC'
chars = '1234567890aabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

nc = Dataset(FILE_NAME,'w',format=FILE_FORMAT)
n2 = 10; nchar = 12; nrecs = 4
nc.createDimension('n1',None)
nc.createDimension('n2',n2)
nc.createDimension('nchar',nchar)
v = nc.createVariable('strings','S1',('n1','n2','nchar'))
for nrec in range(nrecs):
    data = []
    data = numpy.empty((n2,),'S'+repr(nchar))
    for n in range(n2):
        data[n] = ''.join([random.choice(chars) for i in range(nchar)])
    print nrec,data
    datac = stringtochar(data)
    v[nrec] = datac
nc.close()

nc = Dataset(FILE_NAME)
v = nc.variables['strings']
print v.shape, v.dtype
for nrec in range(nrecs):
    print nrec, chartostring(v[nrec])
nc.close()

@@ -204,16 +204,608 @@
 versa.%0A%0A
+# netCDF does not have a fixed-length string data-type (only characters%0A# and variable length strings). The convenience function chartostring%0A# converts an array of characters to an array of fixed-length strings.%0A# The array of fixed length strings has one less dimension, and the%0A# length of the strings is equal to the rightmost dimension of the%0A# array of characters. The convenience function stringtochar goes%0A# the other way, converting an array of fixed-length strings to an%0A# array of characters with an extra dimension (the number of characters%0A# per string) appended on the right.%0A%0A%0A
 FILE_NAM
@@ -1248,16 +1248,68 @@
 nchar))%0A
+    # fill data with random nchar character strings%0A
     for 
@@ -1416,16 +1416,147 @@
 ec,data%0A
+    # convert data to array of characters with an extra dimension %0A    # (the number of characters per string) added to the right.%0A
     data
@@ -1685,16 +1685,16 @@
 v.dtype%0A
-
 for nrec
@@ -1707,24 +1707,138 @@
 nge(nrecs):%0A
+    # read character array back, convert to an array of strings%0A    # of length equal to the rightmost dimension.%0A
     print nr

add some more comments