pjdavis
pjdavis

Reputation: 345

Type conversions on values within a list where there are empty strings

array = [{'@MeasurementDateGMT': '2017-02-25 00:00:00',
          '@SpeciesCode': 'CO',
          '@Value': '0.3'},
         {'@MeasurementDateGMT': '2017-02-25 01:00:00',
          '@SpeciesCode': 'CO',
          '@Value': '0.3'}....}]

try:
    pm10 = [float(d['@Value']) for d in array if d['@SpeciesCode'] == 'PM10']
except ValueError:
    pm10 = [''] * 24

I am using data from a JSON API for plotting in highcharts and so I need to convert the values from strings into floats. The problem is that there are occasional missing values where there is just an empty string. This causes a ValueError, which is why I need the try/except block.

Where there are only just a few missing values, currently the whole set of data will need to replaced (using the except clause), in order to avoid a ValueError for the float conversion.

So for a list such as this:

['3.4', '', '4.0']

I need to end up with a list like this:

[3.4, '', 4.0]

How can I make it so the float conversion is attempted just on those containing values and not on the empty strings?

Upvotes: 0

Views: 51

Answers (2)

Taku
Taku

Reputation: 33724

It appends a empty str if d['@Value'] is ""

#adds d['@Value'] only if its a empty str
pm10 = [d['@Value'] if d['@Value'] == '' else float(d['@Value']) for d in array if d['@SpeciesCode'] == 'PM10']

for example:

array = [{'@MeasurementDateGMT': '2017-02-25 00:00:00',
          '@SpeciesCode': 'CO',
          '@Value': '0.3'},
         {'@MeasurementDateGMT': '2017-02-25 01:00:00',
          '@SpeciesCode': 'PM10',
          '@Value': '0.5'},
         {'@MeasurementDateGMT': '2017-02-25 01:00:00',
          '@SpeciesCode': 'PM10',
          '@Value': ''},
         {'@MeasurementDateGMT': '2017-02-25 01:00:00',
          '@SpeciesCode': 'PM10',
          '@Value': '3.5'}]


pm10 = [d['@Value'] if d['@Value'] == '' else float(d['@Value']) for d in array if '@SpeciesCode' in d.keys() if d['@SpeciesCode'] == 'PM10']

output will be:

[0.5, '', 3.5]

Upvotes: 2

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

that there are occasional missing values where there is just an empty string.

If I understood you correctly, the following simple condition should fit your requirement:

# the example was slightly modified to show empty strings proccessing
array = [{'@MeasurementDateGMT': '2017-02-25 00:00:00',
          '@SpeciesCode': 'PM10',
          '@Value': '0.3'},
         {'@MeasurementDateGMT': '2017-02-25 01:00:00',
          '@SpeciesCode': 'PM10',
          '@Value': ''},
         {'@MeasurementDateGMT': '2017-02-25 01:00:00',
          '@SpeciesCode': 'PM10',
          '@Value': '0.5'}]

pm10 = [float(d['@Value']) if d['@Value'].strip() else '' for d in array if d['@SpeciesCode'] == 'PM10']
print(pm10)

The output:

[0.3, '', 0.5]

Upvotes: 1

Related Questions