Python 2: xml.etree.ElementTree: NCHS Death Rates, Life Expectancy
An if
statement was added to the for
loop because several records had a missing average_life_expectancy
element.
Store the dataset in the same directory where the Python script is stored, then rename it to data.xml
.
To update the dataset, a shell script could remove the old data.xml
file and replace it with a new download:
$ bash get_data.shrm data.xml
wget https://data.cdc.gov/api/views/w9j2-ggv5/rows.xml -O data.xml chown foo:foo data.xml # replace 'foo' with username
chmod 644 data.xml # or 755
Then, in a Unix-like CLI, enter the following:
$ python2 nchs_mortality_expect.py
An example portion of the text attributes from the elements can be seen below:
NCHS: Death Rates, Life Expectancy
Total records: 1043Year: 2014
Race: All Races
Sex: Both Sexes
Average Life Expectancy: 78.9
Mortality: 724.6
— — — — — — — — —
Year: 2013
Race: All Races
Sex: Both Sexes
Average Life Expectancy: 78.8
Mortality: 731.9
— — — — — — — — —
Year: 2012
Race: All Races
Sex: Both Sexes
Average Life Expectancy: 78.8
Mortality: 732.8
— — — — — — — — —
Year: 2011
Race: All Races
Sex: Both Sexes
Average Life Expectancy: 78.7
Mortality: 741.3
— — — — — — — — —...