Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

avioid repetition converting multiple strings to floats

I am running a flask app that effectively scrapes data from https://coronavirus.data.gov.uk/ api and I have a question about formatting (trying not to be repetitive)

So the code in question takes in arguments and parameters for the data and is as follows:

"""covid by nations"""

endpoint_england = (
    'https://api.coronavirus.data.gov.uk/v1/data?'
    'filters=areaType=nation;areaName=england&'
    'structure={"newCases":"newCasesByPublishDate","cumCasesByPublishDate":"cumCasesByPublishDate","dailyDeaths":"newDeaths28DaysByPublishDate","cumulativeDeaths":"cumDeaths28DaysByPublishDate"}&latestBy=cumCasesByPublishDate'
)

cases_england = get(endpoint_england,timeout=10)

if cases_england.status_code >= 400:
    raise RuntimeError(f'Request failed: { cases_england.text }')

cases_england = casesengland.text.replace('}],"requestPayload":{"structure":{"newCases":"newCasesByPublishDate","cumCasesByPublishDate":"cumCasesByPublishDate","dailyDeaths":"newDeaths28DaysByPublishDate","cumulativeDeaths":"cumDeaths28DaysByPublishDate"},"filters":[{"identifier":"areaType","operator":"=","value":"nation"},{"identifier":"areaName","operator":"=","value":"england"}],"latestBy":"cumCasesByPublishDate"}}',"").replace('{"length":1,"maxPageLimit":2500,"totalRecords":1,"data":[{','').replace('newCases','').replace('cumCasesByPublishDate','').replace('dailyDeaths','').replace('cumulativeDeaths','').replace('{','').replace(':','').replace('"','').replace(' ','')
cases_england = cases_england.split(',')

cases_england[0] = '{:20,.0f}'.format(float(cases_england[0]))
cases_england[1] = '{:20,.0f}'.format(float(cases_england[1]))
cases_england[2] = '{:20,.0f}'.format(float(cases_england[2]))
cases_england[3] = '{:20,.0f}'.format(float(cases_england[3]))

@app.route('/')
def covidcases():
    return render_template('dashboard.html', cases_england=cases_england)

I am new to flask and apologies if the code is not great, I am still learning but the code in question is this part:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

cases_england[0] = '{:20,.0f}'.format(float(cases_england[0]))
cases_england[1] = '{:20,.0f}'.format(float(cases_england[1]))
cases_england[2] = '{:20,.0f}'.format(float(cases_england[2]))
cases_england[3] = '{:20,.0f}'.format(float(cases_england[3]))

Because cases_england returns a string and then I split them and the results return a float, I am sure there is a more efficient way of doing this, maybe in a for loop, but I don’t know how to do. I have tried looking but can’t seem to get anywhere.

>Solution :

As a note, I am not entirely certain (since I like to try out code, but can’t execute yours, and I am not sure if I understood your question correctly), but you might try this:

for number, i in enumerate(cases_england):
    cases_england[number] = '{:20,.0f}'.format(float(cases_england[number]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading