Home > Mobile >  Filtering out just decimals form html
Filtering out just decimals form html

Time:08-31

I want to get this html [{'grade_1': '67.81%', 'tag_a_data-analysis': '113087042', 'academic_class': 'Algebra I - 8th Grade - 3 (D)'}, {'grade_2': '71.88%', 'tag_a_data-analysis': '113087763', 'academic_class': 'Computer Science 8 - 1 (E)'}, {'grade_3': '82.40%', 'tag_a_data-analysis': '113086978', 'academic_class': 'English 8/Boys - 3 (G)'}, {'grade_4': '90.77%', 'tag_a_data-analysis': '113087637', 'academic_class': 'Leadership Symphonic Band - 1 (C)'}, {'grade_7': '90.00%', 'tag_a_data-analysis': '113087376', 'academic_class': 'US History - 6 (H)'}] The grade will be differnt every time and turn it into something like

output
grade1 = 67.81
grade2 = 71.88
grade3 = 82.40%
grade4 = 90.00
ect

CodePudding user response:

You can just print out the first key and value in each dictionary:

data = [{'grade_1': '67.81%', 'tag_a_data-analysis': '113087042', 'academic_class': 'Algebra I - 8th Grade - 3 (D)'}, {'grade_2': '71.88%', 'tag_a_data-analysis': '113087763', 'academic_class': 'Computer Science  8 - 1 (E)'}, {'grade_3': '82.40%', 'tag_a_data-analysis': '113086978', 'academic_class': 'English 8/Boys - 3 (G)'}, {'grade_4': '90.77%', 'tag_a_data-analysis': '113087637', 'academic_class': 'Leadership Symphonic Band - 1 (C)'}, {'grade_7': '90.00%', 'tag_a_data-analysis': '113087376', 'academic_class': 'US History - 6 (H)'}]
for element in data: 
        print(list(element.keys())[0], '=', list(element.values())[0])

grade_1 = 67.81%
grade_2 = 71.88%
grade_3 = 82.40%
grade_4 = 90.77%
grade_7 = 90.00%

Relying on dictionary order feels sloppy, but I think the data itself in this object is sloppy, so it's a reasonable solution.

To address your question in the comments, consider instead:

data = [{'grade_1': '67.81%', 'tag_a_data-analysis': '113087042', 'academic_class': 'Algebra I - 8th Grade - 3 (D)'}, {'grade_2': '71.88%', 'tag_a_data-analysis': '113087763', 'academic_class': 'Computer Science  8 - 1 (E)'}, {'grade_3': '82.40%', 'tag_a_data-analysis': '113086978', 'academic_class': 'English 8/Boys - 3 (G)'}, {'grade_4': '90.77%', 'tag_a_data-analysis': '113087637', 'academic_class': 'Leadership Symphonic Band - 1 (C)'}, {'grade_7': '90.00%', 'tag_a_data-analysis': '113087376', 'academic_class': 'US History - 6 (H)'}]
grades={}
for element in data: 
    grades[list(element.keys())[0]] = list(element.values())[0]

print(grades['grade_1'])

67.81%
  • Related