I have a numpy array where each element looks something like this:
[\'3\' \'1\' \'35\' \'0\' \'0\' \'8.05\' \'2\'] [\'3\' \'1\' \'\' \'0\' \'0\' \'8.4583\' \
Here is an approach that uses map:
def FloatOrZero(value): try: return float(value) except: return 0.0 print map(FloatOrZero, ['3', '1', '', '0', '0', '8.4583', '0'])
Outputs:
[3.0, 1.0, 0.0, 0.0, 0.0, 8.4583, 0.0]
In case you needed more flexibility