Nihal
Nihal

Reputation: 31

for loop has saved a list as a single element

I have the following code to extract data from a table but because of the second for loop it saves all the data of a column as a single element of the array

is there a to way separate each element from the array below . link for stat_table : enter image description here

    for table in stat_table:
        for cell in table.find_all('table'):
            stmat.append(cell.text)
            print(cell.text)
            count = count + 1
    print(count)
    print(stmat)
    print(stmat[0])

this is the output where all the data of second loop is saved as a single element

    [' Sep  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24 
     25  26  27  28  29  30 ', ' Max  Avg  Min  82  73.6  70  82  72.9  0  81  74.2  70  84  76.4  
     70  86  75.3  68  82  74.6  68  82  74.6  68  82  74.1  68  81  73.9  68  82  75.4  68  84  
     75.4  68  81  73.9  68  82  75.0  68  79  72.8  68  81  73.6  68  81  73.5  68  82  74.2  68 
     82  74.9  68  82  73.6  68  79  71.9  66  82  72.7  66  81  71.3  63  82  74.1  63  82  75.0  
     64  86  76.4  68  84  75.7  68  82  75.4  68  84  75.5  66  84  74.0  66  86  76.7  66 ', 
    ' Max  Avg  Min  68  66.6  66  68  64.8  0  66  65.2  64  66  65.9  64  68  65.8  64  66  65.3
     64  66  64.7  64  68  66.3  64  70  67.1  64  68  65.9  63  70  66.4  64  68  67.2  66  68  
     66.4  64  68  66.0  64  70  67.4  66  70  67.0  66  68  65.5  64  66  65.4  64  70  67.1  64  
     70  67.1  66  68  65.6  64  66  61.6  59  66  60.3  55  64  60.0  50  66  62.7  59  68  64.8  
     63  68  63.8  61  66  63.9  61  68  64.3  63  68  64.8  61 ', ' Max  Avg  Min  94  80.1  58  
     88  75.1  0  88  75.3  58  88  71.4  51  94  74.0  48  94  74.8  54  94  73.4  54  94  78.4  
     54  100  80.7  58  100  73.9  51  100  76.7  51  100  81.0  61  94  76.0  58  94  80.3  65  
     94  82.5  61  94  81.4  61  94  76.8  54  94  74.4  54  100  82.0  58  100  86.1  65  100  
     80.4  54  100  73.1  48  94  64.6  39  100  62.2  32  88  64.3  40  94  70.4  48  94  69.2  
     48  94  68.8  45  88  73.4  48  94  68.9  43 ', ' Max  Avg  Min  23  15.9  10  22  15.7  10  
     26  15.2  8  20  13.6  8  21  13.6  8  21  13.2  8  22  14.8  9  20  12.2  7  15  10.4  3  
     14  8.8  0  16  10.2  5  14  8.7  1  16  10.9  6  17  12.1  7  17  11.1  6  16  11.2  5  18 
     11.2  5  17  12.4  8  15  10.1  5  15  9.2  3  17  11.6  7  15  9.3  3  12  6.1  0  12  5.2  
     0  10  6.1  0  10  5.8  0  9  4.8  0  10  5.2  0  10  4.5  0  14  4.7  0 ', ' Max  Avg  Min
     26.8  26.7  26.6  26.8  26.1  0.0  26.8  26.8  26.7  26.8  26.8  26.7  26.8  26.8  26.7  26.9  
     26.8  26.7  26.8  26.8  26.7  26.8  26.8  26.7  26.9  26.8  26.8  26.9  26.8  26.7  26.8  26.8  
     26.7  26.8  26.7  26.7  26.8  26.7  26.7  26.8  26.7  26.7  26.8  26.7  26.7  26.8  26.8  26.7  
     26.9  26.8  26.8  26.9  26.8  26.8  26.9  26.9  26.8  26.9  26.9  26.8  26.9  26.8  26.8  26.9  
     26.8  26.8  26.9  26.8  26.8  26.9  26.8  26.8  26.9  26.9  26.8  26.9  26.9  26.8  26.9  26.8
     26.8  26.9  26.8  26.8  26.9  26.8  26.8  26.9  26.8  26.8 ', ' Total  0.00  0.00  0.00  0.00 
     0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
     0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 ']

this is the output of stmat[0] where as I want stmat[0] = sep

    Sep  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30 

Upvotes: 0

Views: 48

Answers (1)

Kevin Eaverquepedo
Kevin Eaverquepedo

Reputation: 652

Given the outputs you show, I'm guessing that

cell.text == "Sep  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24 
     25  26  27  28  29  30 ', ' Max  Avg  Min  82  73.6  70  82  72.9  0  81  74.2  70  84  76.4  
     70  86  75.3  68  82  74.6  68  82  74.6  68  82  74.1  68  81  73.9  68  82  75.4  68  84  
     75.4  68  81  73.9  68  82  75.0  68  79  72.8  68  81  73.6  68  81  73.5  68  82  74.2  68 
     82  74.9  68  82  73.6  68  79  71.9  66  82  72.7  66  81  71.3  63  82  74.1  63  82  75.0  
     64  86  76.4  68  84  75.7  68  82  75.4  68  84  75.5  66  84  74.0  66  86  76.7  66"

So if you want actually individual values, you should probably do something like:

    for table in stat_table:
        for cell in table.find_all('table'):
            cell_values = cell.text.split(" ")
            stmat.extend(cell_values)
            count = count + len(cell_values)
    print(count)
    print(stmat)
    print(stmat[0])

Upvotes: 2

Related Questions