Skip to content

Instantly share code, notes, and snippets.

@GuidoS
Created September 14, 2017 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GuidoS/0a86f54c4338f0558836fb39ad378da6 to your computer and use it in GitHub Desktop.
Save GuidoS/0a86f54c4338f0558836fb39ad378da6 to your computer and use it in GitHub Desktop.
# example non-working attempts to iterate through numpy array and
# strip all the string values
import numpy as np
a = np.array([(u' GNCU ATM', u'CK16 ', u'3a', 1),
(u'EMPLOYMENT SECURITY ', u' CI16', u'3b', 2)],
dtype=[('name', '<U254'), ('map', '<U255'),
('grid', '<U255'), ('OBJECTID', '<i4')])
print 'original: {}\n'.format(a)
# # attempt 1
np.char.strip(a)
# attempt 2
for x in np.nditer(a, op_flags=['readwrite']):
for y in np.nditer(x, op_flags=['readwrite']):
y[...] = y.tostring().strip()
x[...] = x
print 'output: {}'.format(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment